2 using System.Threading;
3 using System.Threading.Tasks;
5 class MyContext : SynchronizationContext
9 public int PostCounter;
10 public int SendCounter;
13 public MyContext (ManualResetEvent mre)
18 public override void OperationStarted ()
21 base.OperationStarted ();
24 public override void OperationCompleted ()
27 base.OperationCompleted ();
30 public override void Post (SendOrPostCallback d, object state)
37 public override void Send (SendOrPostCallback d, object state)
45 public class TestPostContext
47 static ManualResetEvent await_mre;
49 static async Task<int> Test ()
51 return await Task.Factory.StartNew (() => { await_mre.WaitOne(); return 1; });
54 public static int Main ()
56 var mre = new ManualResetEvent (false);
57 await_mre = new ManualResetEvent (false);
58 var context = new MyContext (mre);
60 SynchronizationContext.SetSynchronizationContext (context);
66 // Wait is needed because synchronization is executed as continuation (once task finished)
67 if (!mre.WaitOne (3000))
70 SynchronizationContext.SetSynchronizationContext (null);
73 if (context.Started != 0 || context.Completed != 0 || context.SendCounter != 0)
76 Console.WriteLine ("ok");