Task base syntax
I don’t use this Task very often these days; I spend most of my time improving my skills with Node.js and other applications. Still, the cheat sheet is useful.
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
LOCK
As a basic rule, you need to lock around accessing any writable shared field. Even in the simplest case—an assignment
operation on a single field—you must consider synchronization. In the following class, neither the Increment nor the
Assign method is thread-safe:
*/
class ThreadUnsafe
{
static int _x;
static void Increment() { _x++; }
static void Assign() { _x = 123; }
}
public class TaskSyntax
{
public Task Return_task()=> Task.CompletedTask;
public async Task Return_task_async() => await Task.Yield();
public Task<int> Return_value()=> Task.FromResult(1);
public async Task<int> Return_value_async()
{
// needs at least single method with await
await Task.Yield();
return 1;
}
public async Task Do_waitAll()
{
Task t = new Task(()=> Console.WriteLine(1));
Task t1 = new Task(()=> Console.WriteLine(1));
Task.WaitAll(t,t1);
await Task.Yield();
}
public void Loop_with_var_capture()
{
List<Task> list = new List<Task>();
for (int i = 0; i < 10; i++)
{
int j = i; // capture is nessesary
list.Add(Task.Factory.StartNew(()=> Console.WriteLine(j)));
}
// overwise process in main will be finished before the console prints lines
Task.WaitAll(list.ToArray(),100);
}
public void TaskCreationOptions_usage()
{
Task t1 = new Task(()=> Console.WriteLine(1), TaskCreationOptions.RunContinuationsAsynchronously);
Task t2 = new Task(()=> Console.WriteLine(1), TaskCreationOptions.AttachedToParent);
Task t3 = new Task(()=> Console.WriteLine(1), TaskCreationOptions.DenyChildAttach);
//https://devblogs.microsoft.com/pfxteam/taskcreationoptions-preferfairness/
Task t4 = new Task(()=> Console.WriteLine(1), TaskCreationOptions.PreferFairness);
Task t5 = new Task(()=> Console.WriteLine(1), TaskCreationOptions.HideScheduler);
// Example of TaskCreationOptions.LongRunning usage
/*
protected internal override void QueueTask(Task task)
{
if ((task.Options & TaskCreationOptions.LongRunning) != 0)
{
// Run LongRunning tasks on their own dedicated thread.
Thread thread = new Thread(s_longRunningThreadWork);
thread.IsBackground = true; // Keep this thread from blocking process shutdown
thread.Start(task);
}
else
{
// Normal handling for non-LongRunning tasks.
bool forceToGlobalQueue = (task.Options & TaskCreationOptions.PreferFairness) != 0;
ThreadPool.UnsafeQueueCustomWorkItem(task, forceToGlobalQueue);
}
}
*/
Task t = new Task(() => Console.WriteLine(1), TaskCreationOptions.LongRunning);
}
public async Task Run_async()
{
Task task = Task.Run(async () =>
{
while (true)
{
await Task.Delay(TimeSpan.FromSeconds(10));
}
});
await task;
}
public async Task Run1_async()
{
var task = Task.Run(async () =>
{
await Task.Delay(1);
return 1;
});
var task1 = Task.Run(async () =>
{
await Task.Delay(1);
return 2;
});
int[] r= await Task.WhenAll<int>(task, task1);
Debug.Assert(3== r.Sum());
}
public async Task WhenEach_async()
{
using HttpClient http = new();
Task<string> dotnet = http.GetStringAsync("http://dot.net");
Task<string> bing = http.GetStringAsync("http://www.bing.com");
Task<string> ms = http.GetStringAsync("http://microsoft.com");
await foreach (Task<string> t in Task.WhenEach(bing, dotnet, ms))
{
Console.WriteLine(t.Result);
}
}
public void Status()
{
/*
public enum TaskStatus
{
/// <summary>The task has been initialized but has not yet been scheduled.</summary>
Created,
/// <summary>The task is waiting to be activated and scheduled internally by the .NET infrastructure.</summary>
WaitingForActivation,
/// <summary>The task has been scheduled for execution but has not yet begun executing.</summary>
WaitingToRun,
/// <summary>The task is running but has not yet completed.</summary>
Running,
/// <summary>The task has finished executing and is implicitly waiting for attached child tasks to complete.</summary>
WaitingForChildrenToComplete,
/// <summary>The task completed execution successfully.</summary>
RanToCompletion,
/// <summary>The task acknowledged cancellation by throwing an OperationCanceledException with its own CancellationToken while the token was in signaled state, or the task's CancellationToken was already signaled before the task started executing. For more information, see Task Cancellation.</summary>
Canceled,
/// <summary>The task completed due to an unhandled exception.</summary>
Faulted,
}
*/
Task tsk = Task.Run(() => { });
var status= tsk.Status.ToString();
}
public void Delay()
{
Task tsk1= Task.Delay(100);
Task tsk2= Task.Delay(100);
_ = Task.CurrentId;
Task t = Task.WhenAll(tsk1, tsk2);
Task t1= Task.WhenAny(tsk1, tsk2);
}
public void ContinueWith(Task r)
{
int Foo(Task r)
{
r.ConfigureAwait(false);
return 1;
}
int i= r.ContinueWith(Foo).Result;
Debug.Assert(i==1);
}
public void From()
{
_ = Task.FromCanceled(new CancellationToken(true));
_ = Task.FromException(new Exception());
}
public void Task_To()
{
Task.CompletedTask.RunSynchronously();
}
public void TaskCompletionSource()
{
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(this);
tcs.SetCanceled();
}
}
This post is licensed under CC BY 4.0 by the author.