public async void Support_cancelable_tasks()
        {
            const string taskName = nameof(Support_cancelable_tasks);
            // ReSharper disable once ObjectCreationAsStatement
            0.Invoking(x => new TaskAction((Action<CancellationToken, IProgress<int>>) null, taskName))
                .ShouldThrow<ArgumentNullException>();

            using (var sut = new TaskAction((t,p) => {throw new NotImplementedException();}, taskName))
            {
                await sut.Run().ShouldThrow<NotImplementedException>();
            }

            using (var sut = new TaskAction((t, p) =>
                {
                    for (int i = 1; i <= 10; i++)
                    {
                        t.ThrowIfCancellationRequested();
                        Thread.Sleep(20);
                        p?.Report(i * 10);
                    }
                }, taskName))
            {
                var task = sut.Run(new Progress<int>(p => Trace.TraceInformation($"{p}%")));
                Thread.Sleep(100);
                sut.Cancel();
                await task.ShouldThrow<OperationCanceledException>();
            }
        }
예제 #2
0
        private async Task Run(TaskAction taskAction)
        {
            using (_taskAction = taskAction)
            {
                try
                {
                    WaitingFor = $"Waiting for '{_taskAction.Name}'...";
                    Progress = -1;
                    IsBusy = true;
                    _events.PublishOnUIThread(TaskState.Started);

                    await _taskAction.Run(new Progress<int>(p => Progress = p));

                    _events.PublishOnUIThread(TaskState.Completed);
                }
                catch (OperationCanceledException)
                {
                    _events.PublishOnUIThread(TaskState.Canceled);
                }
                catch (Exception ex)
                {
                    _events.PublishOnUIThread(TaskState.Faulted);
                    var msg = new TaskExceptionMessage(_taskAction.Name, ex);
                    _events.PublishOnUIThread(msg);
                    if (!msg.Handled)
                        throw;
                }
                finally
                {
                    IsBusy = false;
                }
            }
        }
        public void Copy_names()
        {
            const string taskName = nameof(Copy_names);

            // test all ctors
            using (var sut = new TaskAction((c, p) => { }, taskName))
                sut.Name.Should().Be(taskName);

            using (var sut = new TaskAction(() => { }, taskName))
                sut.Name.Should().Be(taskName);
        }
        public async void Support_blocking_threads()
        {
            // ReSharper disable once ObjectCreationAsStatement
            0.Invoking(x => new TaskAction((Action) null, "test"))
                .ShouldThrow<ArgumentNullException>();

            using (var sut = new TaskAction(() =>
            {
                while (true)
                    Thread.Sleep(500);
                // ReSharper disable once FunctionNeverReturns
            }, "test"))
            {
                var task = sut.Run();
                Thread.Sleep(100);
                sut.Cancel();
                await task.ShouldThrow<OperationCanceledException>();
            }
        }