-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
45 lines (43 loc) · 1.56 KB
/
Program.cs
File metadata and controls
45 lines (43 loc) · 1.56 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncExampleNET45
{
class Program
{
static readonly TimeSpan WaitTime = TimeSpan.FromSeconds(20);
static async Task Main(string[] args)
{
var listener = new TextWriterTraceListener(Console.Out);
Trace.Listeners.Add(listener);
using (var TokenSource = new CancellationTokenSource())
using (ConsoleUtils.SetConsoleCtrl.Create(type => {
TokenSource?.Cancel();
return false;
}))
await MainAction(TokenSource.Token);
}
static async Task MainAction(CancellationToken Token)
{
DateTime StartTime = DateTime.Now;
await Task.Factory.StartNew(() => Task2Action(StartTime, Token)).Unwrap();
Trace.WriteLine($"Task1 {DateTime.Now - StartTime}");
await Task.Run(() => Task4Action(StartTime, Token));
Trace.WriteLine($"Task3 {DateTime.Now - StartTime}");
}
static async Task Task2Action(DateTime StartTime, CancellationToken Token)
{
await Task.Delay(WaitTime, Token);
Trace.WriteLine($"Task2 {DateTime.Now - StartTime}");
}
static async Task Task4Action(DateTime StartTime, CancellationToken Token)
{
await Task.Delay(WaitTime, Token);
Trace.WriteLine($"Task4 {DateTime.Now - StartTime}");
}
}
}