[System.Data] Try to fix random DataViewTest.DefaultColumnNameAddListChangedTest...
[mono-project.git] / mcs / tests / test-async-55.cs
blobf28a4e01aab1512fb5cb5accb97f082be496d456
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
5 class MyContext : SynchronizationContext
7 public override void Post (SendOrPostCallback d, object state)
9 base.Post (d, state);
12 public override void Send (SendOrPostCallback d, object state)
14 base.Send (d, state);
18 class X
20 static TaskCompletionSource<bool> tcs;
21 static ManualResetEvent mre, mre2;
22 static int main_thread_id;
24 public static int Main ()
26 main_thread_id = Thread.CurrentThread.ManagedThreadId;
27 Console.WriteLine ("{0}:Main start", main_thread_id);
29 mre = new ManualResetEvent (false);
30 mre2 = new ManualResetEvent (false);
31 tcs = new TaskCompletionSource<bool> ();
33 Task.Factory.StartNew (new Func<Task> (ExecuteAsync), new CancellationToken (), TaskCreationOptions.LongRunning, TaskScheduler.Default);
35 if (!mre.WaitOne (1000))
36 return 1;
38 // Have to wait little bit longer for await not to take quick path
39 Thread.Sleep (10);
41 Console.WriteLine ("{0}:Main Set Result", Thread.CurrentThread.ManagedThreadId);
43 SynchronizationContext.SetSynchronizationContext (new MyContext ());
45 tcs.SetResult (true);
47 if (!mre2.WaitOne (1000))
48 return 2;
50 Console.WriteLine ("ok");
51 return 0;
54 static async Task ExecuteAsync ()
56 var t = Thread.CurrentThread;
57 Console.WriteLine ("{0} - started ", t.ManagedThreadId);
59 mre.Set ();
61 await tcs.Task;
62 t = Thread.CurrentThread;
63 Console.WriteLine ("{0} - resumed ", t.ManagedThreadId);
66 // Continuation cannot resume on main thread because it has synchronization context set
68 if (main_thread_id != Thread.CurrentThread.ManagedThreadId)
69 mre2.Set ();