public static void EnlistWithSyncContext_BeforeCancel_ThrowingExceptionInSyncContextDelegate_ThrowOnFirst()
        {
            ManualResetEvent mre_CancelHasBeenEnacted = new ManualResetEvent(false); //synchronization helper

            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;


            // Install a SynchronizationContext...
            SynchronizationContext prevailingSyncCtx = SynchronizationContext.Current;
            TestingSynchronizationContext testContext = new TestingSynchronizationContext();
            SetSynchronizationContext(testContext);

            // Main test body 
            ArgumentException caughtException = null;

            // register a null delegate, but use the currently registered syncContext.
            // the testSyncContext will track that it was used when the delegate is invoked.
            token.Register(() => { throw new ArgumentException(); }, true);

            Task.Run(
                () =>
                {
                    try
                    {
                        tokenSource.Cancel(true);
                    }
                    catch (ArgumentException ex)
                    {
                        caughtException = ex;
                    }
                    mre_CancelHasBeenEnacted.Set();
                }
                );

            mre_CancelHasBeenEnacted.WaitOne();
            Assert.True(testContext.DidSendOccur,
               "EnlistWithSyncContext_BeforeCancel_ThrowingExceptionInSyncContextDelegate_ThrowOnFirst:  the delegate should have been called via Send to SyncContext.");
            Assert.NotNull(caughtException);

            //Cleanup
            SetSynchronizationContext(prevailingSyncCtx);
        }
        public static void EnlistWithSyncContext_BeforeCancel()
        {
            ManualResetEvent mre_CancelHasBeenEnacted = new ManualResetEvent(false); //synchronization helper

            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;


            // Install a SynchronizationContext...
            SynchronizationContext prevailingSyncCtx = SynchronizationContext.Current;
            TestingSynchronizationContext testContext = new TestingSynchronizationContext();
            SetSynchronizationContext(testContext);

            // Main test body 

            // register a null delegate, but use the currently registered syncContext.
            // the testSyncContext will track that it was used when the delegate is invoked.
            token.Register(() => { }, true);

            Task.Run(
                () =>
                {
                    tokenSource.Cancel();
                    mre_CancelHasBeenEnacted.Set();
                }
                );

            mre_CancelHasBeenEnacted.WaitOne();
            Assert.True(testContext.DidSendOccur,
               "EnlistWithSyncContext_BeforeCancel:  the delegate should have been called via Send to SyncContext.");

            //Cleanup.
            SetSynchronizationContext(prevailingSyncCtx);
        }