-
Notifications
You must be signed in to change notification settings - Fork 13
/
Program.cs
43 lines (35 loc) · 1.17 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
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConfigureAwait
{
class Program
{
static void Main(string[] args)
{
Run().GetAwaiter().GetResult();
}
static async Task Run()
{
var syncContext = new MySyncContext { State = "The Original Context" };
SynchronizationContext.SetSynchronizationContext(syncContext);
Console.WriteLine("Original:" + SynchronizationContext.Current);
await Task.Delay(1000).ConfigureAwait(true);
Console.WriteLine("After Result1:" + SynchronizationContext.Current);
await Task.Delay(1000);
Console.WriteLine("After Result2:" + SynchronizationContext.Current);
}
}
public class MySyncContext : SynchronizationContext
{
public string State { get; set; }
public override void Post(SendOrPostCallback callback, object state)
{
base.Post(s => {
SynchronizationContext.SetSynchronizationContext(this);
callback(s);
}, state);
}
public override string ToString() => State;
}
}