Skip to content

Commit 5002fc0

Browse files
author
Gavin Lang
committed
clear up some concepts
1 parent 2e9e8d5 commit 5002fc0

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

ConsoleApp.TDF.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
<DebugType>pdbonly</DebugType>
1717
</PropertyGroup>
1818
<ItemGroup>
19-
<PackageReference Include="Microsoft.Tpl.Dataflow" Version="4.5.24" />
2019
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
2120
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
2221
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
2322
<PackageReference Include="Microsoft.CSharp" Version="4.5.0" />
23+
<PackageReference Include="System.Threading.Tasks.Dataflow" Version="4.11.0" />
2424
</ItemGroup>
2525
</Project>

Program.cs

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,50 @@ namespace ConsoleApp.TDF
1010
{
1111
internal class Program
1212
{
13-
private static void Main()
13+
private static async Task Main()
1414
{
1515
Log.Logger = new LoggerConfiguration()
1616
.Enrich.WithThreadId()
1717
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} {ThreadId} {Message}{NewLine}")
1818
.CreateLogger();
1919

20-
Log.Information("Main()");
20+
Log.Information("Begin");
2121

2222
var head = new BufferBlock<int>();
23-
var workActionBlock = new ActionBlock<object>(i =>
23+
var actionBlock = new ActionBlock<object>(i =>
2424
{
25-
Log.Information("ActionBlock({i})", i);
26-
Thread.Sleep(TimeSpan.FromSeconds(2));
25+
Log.Information("ActionBlock({i}) starts", i);
26+
Thread.Sleep(TimeSpan.FromSeconds(1));
27+
Log.Information("ActionBlock({i}) finished", i);
2728
}, new ExecutionDataflowBlockOptions
2829
{
29-
MaxDegreeOfParallelism = 2
30+
MaxDegreeOfParallelism = 1
3031
});
3132

3233
var transformBlock = new TransformBlock<int, string>(Transform, new ExecutionDataflowBlockOptions
3334
{
34-
//MaxDegreeOfParallelism = 1
35+
MaxDegreeOfParallelism = 2
3536
});
3637

3738
//head.LinkTo(workActionBlock);
3839
head.LinkTo(transformBlock);
39-
transformBlock.LinkTo(workActionBlock);
40+
transformBlock.LinkTo(actionBlock);
41+
Log.Information("head => transformBlock => actionBlock");
42+
4043
head.Completion.ContinueWith(t => transformBlock.Complete());
41-
transformBlock.Completion.ContinueWith(t => workActionBlock.Complete());
44+
transformBlock.Completion.ContinueWith(t => actionBlock.Complete());
4245

4346
var sw = new Stopwatch();
4447
sw.Start();
4548
foreach (var i in Enumerable.Range(0, 10))
4649
{
47-
//head.Post(i);
48-
head.SendAsync(i);
50+
head.Post(i);
51+
//await head.SendAsync(i);
4952
}
5053

5154
head.Complete();
52-
53-
Task.WaitAll(head.Completion, workActionBlock.Completion);
55+
await head.Completion;
56+
await actionBlock.Completion;
5457

5558
sw.Stop();
5659

@@ -60,9 +63,12 @@ private static void Main()
6063

6164
private static string Transform(int i)
6265
{
63-
Log.Information("Transform({i}) => {result}", i, i.ToString());
66+
Log.Information("Transform({i}) starts work on {i}", i, i);
67+
var result = i.ToString();
6468
Thread.Sleep(TimeSpan.FromSeconds(1));
65-
return i.ToString();
69+
70+
Log.Information("Transform({i}) finished work on {i} with result {result}", i, i, result);
71+
return result;
6672
}
6773
}
6874
}

0 commit comments

Comments
 (0)