-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (62 loc) · 2.3 KB
/
Program.cs
File metadata and controls
74 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Serilog;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace ConsoleApp.TDF
{
internal class Program
{
private static async Task Main()
{
Log.Logger = new LoggerConfiguration()
.Enrich.WithThreadId()
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} {ThreadId} {Message}{NewLine}")
.CreateLogger();
Log.Information("Begin");
var head = new BufferBlock<int>();
var actionBlock = new ActionBlock<object>(i =>
{
Log.Information("ActionBlock({i}) starts", i);
Thread.Sleep(TimeSpan.FromSeconds(1));
Log.Information("ActionBlock({i}) finished", i);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 1
});
var transformBlock = new TransformBlock<int, string>(Transform, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 2
});
//head.LinkTo(workActionBlock);
head.LinkTo(transformBlock);
transformBlock.LinkTo(actionBlock);
Log.Information("head => transformBlock => actionBlock");
head.Completion.ContinueWith(t => transformBlock.Complete());
transformBlock.Completion.ContinueWith(t => actionBlock.Complete());
var sw = new Stopwatch();
sw.Start();
foreach (var i in Enumerable.Range(0, 10))
{
head.Post(i);
//await head.SendAsync(i);
}
head.Complete();
await head.Completion;
await actionBlock.Completion;
sw.Stop();
Log.Information("Finished in {elapsed}", sw.Elapsed);
Log.CloseAndFlush();
}
private static string Transform(int i)
{
Log.Information("Transform({i}) starts work on {i}", i, i);
var result = i.ToString();
Thread.Sleep(TimeSpan.FromSeconds(1));
Log.Information("Transform({i}) finished work on {i} with result {result}", i, i, result);
return result;
}
}
}