About async/await

Difference between running a Func<Task> and Task.Run(). See this article.

Action vs Func<Task>.

  • Running a Func<Task> runs the first part of its body synchronously up until the first await statement. The rest part are run on a separate task.
  • Task.Run() puts its input to a separate task to run.

The below two snippets are trivially different. TODO need elaborating.

1
2
3
4
5
6
7
8
9
var tasks = Enumerable.Range(0, 100).Select(async x =>
{
log("step1");
await Task.Delay(1000);
log("step2");
}).ToArray();

log("step3");
Task.WaitAll(tasks);

compared to:

1
2
3
4
5
6
7
8
9
var tasks = Enumerable.Range(0, 100).Select(x => Task.Run(async () =>
{
log("step1");
await Task.Delay(1000);
log("step2");
})).ToArray();

log("step3");
Task.WaitAll(tasks);

About SynchronizationContext

It's an abstraction interface whose implementations are used to dispatch asynchronous tasks in some specific ways. See this article.