public void SingleThreadSyncContextShouldRunOnOneThread1()
        {
            var t = Task.Factory.StartNew(() =>
            {
                var prevContext = SynchronizationContext.Current;
                try
                {
                    Trace.WriteLine("SingleThreadSyncContextShouldRunOnOneThread1: ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);
                    var context = new SingleThreadSynchronizationContext();
                    SynchronizationContext.SetSynchronizationContext(context);
                    Func <Task <Dictionary <int, int> > > func = async delegate { return(await TaskYieldProcess()); };
                    var yieldTask = func();
                    yieldTask.ContinueWith(delegate { context.Complete(); }, TaskScheduler.Default);
                    context.RunOnCurrentThread();
                    bool success = yieldTask.Wait(TimeSpan.FromSeconds(10.0));
                    Assert.IsTrue(success, "Wait for yield method using SingleThreadSynchronizationContext failed");
                    return(yieldTask.Result);
                }
                finally
                {
                    SynchronizationContext.SetSynchronizationContext(prevContext);
                }
            });

            t.Wait();
            var result = t.Result;

            Assert.AreEqual(1, result.Count, "Should run on exactly 1 thread");
        }
 public void SingleThreadSyncContextShouldRunOnOneThread1()
 {
     var t = Task.Factory.StartNew(() =>
         {
             var prevContext = SynchronizationContext.Current;
             try
             {
                 Trace.WriteLine("SingleThreadSyncContextShouldRunOnOneThread1: ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);
                 var context = new SingleThreadSynchronizationContext();
                 SynchronizationContext.SetSynchronizationContext(context);
                 Func<Task<Dictionary<int, int>>> func = async delegate { return await TaskYieldProcess(); };
                 var yieldTask = func();
                 yieldTask.ContinueWith(delegate { context.Complete(); }, TaskScheduler.Default);
                 context.RunOnCurrentThread();
                 bool success = yieldTask.Wait(TimeSpan.FromSeconds(10.0));
                 Assert.IsTrue(success, "Wait for yield method using SingleThreadSynchronizationContext failed");
                 return yieldTask.Result;
             }
             finally
             {
                 SynchronizationContext.SetSynchronizationContext(prevContext);
             }
         });
     t.Wait();
     var result = t.Result;
     Assert.AreEqual(1, result.Count, "Should run on exactly 1 thread");
 }
        public static void Run(Func <Task> func)
        {
            var prevCtx = SynchronizationContext.Current;

            try
            {
                var syncCtx = new SingleThreadSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(syncCtx);
                Trace.WriteLine("Run: ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);

                var t = func();
                t.ContinueWith(
                    delegate { syncCtx.Complete(); }, TaskScheduler.Default);

                syncCtx.RunOnCurrentThread();
                t.GetAwaiter().GetResult();
            }
            finally { SynchronizationContext.SetSynchronizationContext(prevCtx); }
        }
        public static void Run(Func<Task> func)
        {
            var prevCtx = SynchronizationContext.Current;
            try
            {
                var syncCtx = new SingleThreadSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(syncCtx);
                Trace.WriteLine("Run: ManagedThreadId = " + Thread.CurrentThread.ManagedThreadId);

                var t = func();
                t.ContinueWith(
                    delegate { syncCtx.Complete(); }, TaskScheduler.Default);

                syncCtx.RunOnCurrentThread();
                t.GetAwaiter().GetResult();
            }
            finally { SynchronizationContext.SetSynchronizationContext(prevCtx); }
        }