コード例 #1
0
        private static bool EnlistWithSyncContext_BeforeCancel_ThrowingExceptionInSyncContextDelegate_ThrowOnFirst()
        {
            TestHarness.TestLog("* CancellationTokenTests.EnlistWithSyncContext_BeforeCancel_ThrowingExceptionInSyncContextDelegate()");
            bool passed = true;
            ManualResetEventSlim mres_CancelHasBeenEnacted = new ManualResetEventSlim(); //synchronization helper

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


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

            // Main test body 
            ApplicationException 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 ApplicationException(); }, true);

            ThreadPool.QueueUserWorkItem(
                (state) =>
                {
                    try
                    {
                        tokenSource.Cancel(true);
                    }
                    catch (ApplicationException ex)
                    {
                        caughtException = ex;
                    }
                    mres_CancelHasBeenEnacted.Set();
                }
                );

            mres_CancelHasBeenEnacted.Wait();
            passed &= TestHarnessAssert.IsTrue(testContext.DidSendOccur, "the delegate should have been called via Send to SyncContext.");
            passed &= TestHarnessAssert.IsNotNull(caughtException, "An ApplicationException should be thrown.");

            //Cleanup
            SynchronizationContext.SetSynchronizationContext(prevailingSyncCtx);

            return passed;
        }
コード例 #2
0
        private static bool EnlistWithSyncContext_BeforeCancel()
        {
            TestHarness.TestLog("* CancellationTokenTests.EnlistWithSyncContext_BeforeCancel()");
            bool passed = true;
            ManualResetEventSlim mres_CancelHasBeenEnacted = new ManualResetEventSlim(); //synchronization helper

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

            
            // Install a SynchronizationContext...
            TestingSynchronizationContext testContext = new TestingSynchronizationContext();
            SynchronizationContext.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);

            ThreadPool.QueueUserWorkItem(
                (state) =>
                    {
                        tokenSource.Cancel();
                        mres_CancelHasBeenEnacted.Set();
                    }
                );

            mres_CancelHasBeenEnacted.Wait();
            passed &= TestHarnessAssert.IsTrue(testContext.DidSendOccur, "the delegate should have been called via Send to SyncContext.");

            return passed;
        }