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); } } }
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. }