Esempio n. 1
0
        public async Task AsyncOperationCancelledIfRequestedAsync()
        {
            await _taskFactory.SwitchToMainThreadAsync();

            var asyncOperationStarted = new AsyncManualResetEvent();
            var taskCancelled         = new AsyncManualResetEvent();
            var source = new CancellationTokenSource();

            Task asyncOperation = _taskExecutor.SubmitAsync(async() =>
            {
                asyncOperationStarted.Set();
                await taskCancelled.WaitAsync();
                Assert.Throws <OperationCanceledException>(
                    () => _taskExecutor.CancelAsyncOperationIfRequested());
            }, source.Token, _dummyMethodName, _dummyType);

            await asyncOperationStarted.WaitAsync();

            source.Cancel();
            taskCancelled.Set();

            Assert.ThrowsAsync <TaskCanceledException>(async() => await asyncOperation);
        }