Пример #1
0
        public async Task ExecuteNotifiesTaskStatus()
        {
            // http://stackoverflow.com/questions/34811639/is-there-a-way-to-be-notified-when-task-status-changes-to-running
            var tcs = new TaskCompletionSource <int>();

            using var command = new AsyncCommand(() => tcs.Task);
            var taskStatuses = new List <TaskStatus>();

            using (command.ObserveValue(x => x.Execution.Status)
                   .Subscribe(x => taskStatuses.Add(x.Value)))
            {
                Assert.IsFalse(command.IsExecuting);
                Assert.IsFalse(command.CancelCommand.CanExecute());
                command.Execute();
                Assert.IsTrue(command.IsExecuting);
                Assert.IsFalse(command.CancelCommand.CanExecute());
                var expectedStatuses = new List <TaskStatus> {
                    TaskStatus.Created, TaskStatus.WaitingForActivation, TaskStatus.Running,
                };
                CollectionAssert.AreEqual(expectedStatuses, taskStatuses);
                tcs.SetResult(1);
                await command.Execution !.Task.ConfigureAwait(false);
                Assert.IsFalse(command.IsExecuting);
                expectedStatuses.Add(TaskStatus.RanToCompletion);
                CollectionAssert.AreEqual(expectedStatuses, taskStatuses);
            }
        }