Ejemplo n.º 1
0
 public void SwitchOffMainThreadAsync_CanceledBeforeSwitch()
 {
     using (CancellationTokenSource cts = new CancellationTokenSource())
     {
         cts.Cancel();
         try
         {
             AsynchronityUtilities.SwitchOffMainThreadAsync(cts.Token);
             Assert.Fail("Expected OperationCanceledException not thrown.");
         }
         catch (OperationCanceledException ex)
         {
             Assert.AreEqual(cts.Token, ex.CancellationToken);
         }
     }
 }
Ejemplo n.º 2
0
        public void SwitchOffMainThreadAsync_CanceledMidSwitch()
        {
            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                AsynchronityUtilities.TaskSchedulerAwaiter awaitable = AsynchronityUtilities.SwitchOffMainThreadAsync(cts.Token);
                AsynchronityUtilities.TaskSchedulerAwaiter awaiter   = awaitable.GetAwaiter();

                cts.Cancel();

                try
                {
                    awaiter.GetResult();
                    Assert.Fail("Expected OperationCanceledException not thrown.");
                }
                catch (OperationCanceledException ex)
                {
                    Assert.AreEqual(cts.Token, ex.CancellationToken);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task SwitchOffMainThreadAsync_OnMainThread()
        {
            // Make this thread look like the main thread by
            // setting up a synchronization context.
            SynchronizationContext dispatcher = new SynchronizationContext();
            SynchronizationContext original   = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(dispatcher);
            try
            {
                Thread originalThread = Thread.CurrentThread;

                await AsynchronityUtilities.SwitchOffMainThreadAsync(CancellationToken.None);

                Assert.AreNotSame(originalThread, Thread.CurrentThread);
                Assert.IsNull(SynchronizationContext.Current);
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(original);
            }
        }
Ejemplo n.º 4
0
 public void SwitchOffMainThreadAsync_OffMainThread()
 {
     AsynchronityUtilities.TaskSchedulerAwaiter awaitable = AsynchronityUtilities.SwitchOffMainThreadAsync(CancellationToken.None);
     AsynchronityUtilities.TaskSchedulerAwaiter awaiter   = awaitable.GetAwaiter();
     Assert.IsTrue(awaiter.IsCompleted); // guarantees the caller wouldn't have switched threads.
 }