public void TestBackgroundCancelMultipleActions() { // Ensure that multiple background actions are cancelled if they // use the same cancellation token. var listener = new AggregateAsynchronousOperationListener(Enumerable.Empty<Lazy<IAsynchronousOperationListener, FeatureMetadata>>(), "Test"); var worker = new AsynchronousSerialWorkQueue(listener); var taskRunningEvent = new AutoResetEvent(false); var cancelEvent = new AutoResetEvent(false); var doneEvent = new AutoResetEvent(false); var source = new CancellationTokenSource(); var cancellationToken = source.Token; var action1Ran = false; var action2Ran = false; worker.EnqueueBackgroundWork(() => { action1Ran = true; Assert.NotSame(_foregroundSyncContext, SynchronizationContext.Current); Assert.False(cancellationToken.IsCancellationRequested); taskRunningEvent.Set(); cancelEvent.WaitOne(); cancellationToken.ThrowIfCancellationRequested(); Assert.True(false); }, "Test", source.Token); // We should not run this action. worker.EnqueueBackgroundWork(() => { action2Ran = true; Assert.False(true); }, "Test", source.Token); taskRunningEvent.WaitOne(); source.Cancel(); cancelEvent.Set(); try { worker.WaitUntilCompletion_ForTestingPurposesOnly(); Assert.True(false); } catch (AggregateException ae) { Assert.IsAssignableFrom<OperationCanceledException>(ae.InnerException); } Assert.True(action1Ran); Assert.False(action2Ran); }