@@ -10,47 +10,50 @@ namespace ConsoleApp.TDF
10
10
{
11
11
internal class Program
12
12
{
13
- private static void Main ( )
13
+ private static async Task Main ( )
14
14
{
15
15
Log . Logger = new LoggerConfiguration ( )
16
16
. Enrich . WithThreadId ( )
17
17
. WriteTo . Console ( outputTemplate : "{Timestamp:HH:mm:ss} {ThreadId} {Message}{NewLine}" )
18
18
. CreateLogger ( ) ;
19
19
20
- Log . Information ( "Main() " ) ;
20
+ Log . Information ( "Begin " ) ;
21
21
22
22
var head = new BufferBlock < int > ( ) ;
23
- var workActionBlock = new ActionBlock < object > ( i =>
23
+ var actionBlock = new ActionBlock < object > ( i =>
24
24
{
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 ) ;
27
28
} , new ExecutionDataflowBlockOptions
28
29
{
29
- MaxDegreeOfParallelism = 2
30
+ MaxDegreeOfParallelism = 1
30
31
} ) ;
31
32
32
33
var transformBlock = new TransformBlock < int , string > ( Transform , new ExecutionDataflowBlockOptions
33
34
{
34
- // MaxDegreeOfParallelism = 1
35
+ MaxDegreeOfParallelism = 2
35
36
} ) ;
36
37
37
38
//head.LinkTo(workActionBlock);
38
39
head . LinkTo ( transformBlock ) ;
39
- transformBlock . LinkTo ( workActionBlock ) ;
40
+ transformBlock . LinkTo ( actionBlock ) ;
41
+ Log . Information ( "head => transformBlock => actionBlock" ) ;
42
+
40
43
head . Completion . ContinueWith ( t => transformBlock . Complete ( ) ) ;
41
- transformBlock . Completion . ContinueWith ( t => workActionBlock . Complete ( ) ) ;
44
+ transformBlock . Completion . ContinueWith ( t => actionBlock . Complete ( ) ) ;
42
45
43
46
var sw = new Stopwatch ( ) ;
44
47
sw . Start ( ) ;
45
48
foreach ( var i in Enumerable . Range ( 0 , 10 ) )
46
49
{
47
- // head.Post(i);
48
- head . SendAsync ( i ) ;
50
+ head . Post ( i ) ;
51
+ //await head.SendAsync(i);
49
52
}
50
53
51
54
head . Complete ( ) ;
52
-
53
- Task . WaitAll ( head . Completion , workActionBlock . Completion ) ;
55
+ await head . Completion ;
56
+ await actionBlock . Completion ;
54
57
55
58
sw . Stop ( ) ;
56
59
@@ -60,9 +63,12 @@ private static void Main()
60
63
61
64
private static string Transform ( int i )
62
65
{
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 ( ) ;
64
68
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 ;
66
72
}
67
73
}
68
74
}
0 commit comments