-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
153 lines (119 loc) · 4.14 KB
/
Program.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsyncSimple.Classes;
using static AsyncSimple.Classes.GlobalStuff;
using TimeSpan = System.TimeSpan;
namespace AsyncSimple
{
class Program
{
private static readonly CancellationTokenSource _cancellationSource = new();
private static readonly ImportItem _importItem = new () {RowsStr = "Start"};
static async Task Main(string[] args)
{
Console.WriteLine($"RowStr = '{_importItem.RowsStr}'");
await Go_ImportCommand_Execute();
Console.WriteLine($"RowStr = '{_importItem.RowsStr}'");
Console.WriteLine("2");
Console.Title = "Async simple/basics";
Console.WriteLine("Return bool");
var example1Result = await Example1();
Console.WriteLine($"\t{example1Result.ToYesNo()}");
Console.WriteLine();
Console.WriteLine("Return list");
var customersList = await Example2();
foreach (var customer in customersList)
{
Console.WriteLine($"\t{customer}");
}
Console.WriteLine();
Console.WriteLine("Iterator");
await Example3();
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(2);
Console.WriteLine();
Console.WriteLine("Timer");
var timer = new Timer(async (e) =>
{
var x = await Example1();
Console.WriteLine(x.ToYesNo());
}, null, startTimeSpan, periodTimeSpan);
await Task.Delay(3000);
Console.WriteLine();
Console.WriteLine("WhenAll");
await Example4();
Console.WriteLine();
Console.ReadLine();
}
public static async Task<bool> Example1()
{
return await Task.Run(async () =>
{
await Task.Delay(1000);
return true;
});
}
public static async Task<List<Customers>> Example2()
{
return await Task.Run(async () =>
{
await Task.Delay(1);
return MockedData.MockedInMemoryCustomers();
});
}
private static async Task Example3()
{
try
{
await foreach (var item in 10.RangeAsync(MaxNumber, _cancellationSource.Token).WithCancellation(_cancellationSource.Token))
{
Console.WriteLine($"\t{item}");
}
;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static async Task Example4()
{
// this task will take about 2.5s to complete
var sumTask = SlowAndComplexSumAsync();
// this task will take about 4s to complete
var wordTask = SlowAndComplexWordAsync();
// running them in parallel should take about 4s to complete
await Task.WhenAll(sumTask, wordTask);
Console.WriteLine($"Result of complex sum = {sumTask.Result}");
Console.WriteLine("Result of complex letter processing {wordTask.Result");
}
private static async Task<int> SlowAndComplexSumAsync()
{
int sum = 0;
foreach (var counter in Enumerable.Range(0, 25))
{
sum += counter;
await Task.Delay(100);
}
return sum;
}
private static async Task<string> SlowAndComplexWordAsync()
{
var word = string.Empty;
foreach (var counter in Enumerable.Range(65, 26))
{
word = string.Concat(word, (char)counter);
await Task.Delay(150);
}
return word;
}
private static async Task Go_ImportCommand_Execute()
{
_importItem.RowsStr = "";
await Task.Delay(2000);
}
}
}