public void AllowMultipleExecutionTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a, o => true) { AllowMultipleExecution = true }; EnqueueCallback(() => { asyncTestCommand.Execute(100); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); }); EnqueueWait(() => asyncTestCommand.executeTask.IsCompleted); EnqueueWait(() => !asyncTestCommand.IsExecuting); EnqueueCallback(() => { Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand = new AsyncCommand <int>(a, o => false) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); }); EnqueueTestComplete(); }
void AllowMultipleExecutionTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a, o => true) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand.Wait(latencyTime); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand = new AsyncCommand <int>(a, o => false) { AllowMultipleExecution = true }; asyncTestCommand.Execute(100); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); }
private void _progressDialog_Cancelled(object sender, EventArgs e) { if (Cancelled != null) { Cancelled.Invoke(this, null); //REVIEW jh wesay } _currentCommand.Cancel(); }
public SecondViewModel(INavigationService navigationService) { NavigationService = navigationService; // Устанавливаем команду для асинхронной инициализации InitAsyncCommand = new AsyncCommand <string>(InitAsync); GoToExceptionCommand = new AsyncCommand(_ => navigationService.NavigateToAsync <ExceptionViewModel>()); CloseCommand = new AsyncCommand(_ => { // Не забываем отменить асинхронную инициализацию при закрытии страницы // Вдруг она еще не отработала InitAsyncCommand.Cancel(); return(navigationService.CloseAsync(this)); }); }
void AsyncCancelTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a); Assert.IsNull(asyncTestCommand.CancellationTokenSource); asyncTestCommand.Execute(100); Assert.IsNotNull(asyncTestCommand.CancellationTokenSource); Assert.IsTrue(asyncTestCommand.IsExecuting); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.ShouldCancel); Assert.IsTrue(asyncTestCommand.IsCancellationRequested); Assert.IsTrue(asyncTestCommand.IsExecuting); asyncTestCommand.Wait(latencyTime); Assert.IsTrue(asyncTestCommand.executeTask.IsCompleted); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.ShouldCancel); Assert.IsTrue(asyncTestCommand.IsCancellationRequested); }
public ExceptionViewModel(INavigationService navigationService) { NavigationService = navigationService; // Устанавливаем команду для асинхронной инициализации с обработчиком ошибки InitAsyncCommand = new AsyncCommand(InitAsync).WithFaultedHandlerAsync(FaultedHandlerAsync); // Обратить внимание запуск в конструкторе и без await! // Это норм - обработка исключения внутри команды InitAsyncCommand.ExecuteAsync(); CloseCommand = new AsyncCommand(_ => { // Не забываем отменить асинхронную инициализацию при закрытии страницы // Вдруг она еще не отработала InitAsyncCommand.Cancel(); return(navigationService.CloseAsync(this)); }); }
void AsyncCanExecuteTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a, o => true); asyncTestCommand.Execute(100); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.ShouldCancel); Assert.IsTrue(asyncTestCommand.IsCancellationRequested); asyncTestCommand.Wait(latencyTime); Assert.IsTrue(asyncTestCommand.executeTask.IsCompleted); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsTrue(asyncTestCommand.CanExecute(100)); asyncTestCommand = new AsyncCommand <int>(a, o => false); asyncTestCommand.Execute(100); Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.CanExecute(100)); }
public void AsyncCancelTestCore(Func <int, Task> a) { asyncTestCommand = new AsyncCommand <int>(a); Assert.IsNull(asyncTestCommand.CancellationTokenSource); EnqueueCallback(() => { asyncTestCommand.Execute(100); Assert.IsNotNull(asyncTestCommand.CancellationTokenSource); Assert.IsTrue(asyncTestCommand.IsExecuting); asyncTestCommand.Cancel(); Assert.IsTrue(asyncTestCommand.ShouldCancel); Assert.IsTrue(asyncTestCommand.IsCancellationRequested); Assert.IsTrue(asyncTestCommand.IsExecuting); }); EnqueueWait(() => asyncTestCommand.executeTask.IsCompleted); EnqueueWait(() => !asyncTestCommand.IsExecuting); EnqueueCallback(() => { Assert.IsFalse(asyncTestCommand.IsExecuting); Assert.IsFalse(asyncTestCommand.ShouldCancel); Assert.IsTrue(asyncTestCommand.IsCancellationRequested); }); EnqueueTestComplete(); }
public void ManualCancelCommandTest() { var logs = new List <string>(); var command = new AsyncCommand(Execute).WithCancelledHandlerAsync(Cancelled); // Команда выполняется 4 секунды и что-то асинхронно делает // По окончании пишем в логгер об успехе Task Execute(CancellationToken token) => Task.Run(async() => { for (var i = 0; i < 40; i++) { await Task.Delay(100, token); } logs.Add("Command completed successfully"); }, token); // Пишем в логгер, если команда отменилась Task Cancelled(OperationCanceledException exception) => Task.Run(() => logs.Add("Command canceled")); // Запускаем команду command.ExecuteAsync(); // Ждем 1 секунду Thread.Sleep(1000); // Недождались, отменяем command.Cancel(); // Ждем еще немного, чтобы успело сгенерироваться и обработаться исключение Thread.Sleep(500); Assert.AreEqual(1, logs.Count); Assert.AreEqual("Command canceled", logs.Single()); Assert.False(command.IsExecuting); }