public void Run(object parameter) { try { Status = AsyncNotificationStatus.Busy; execute.Invoke(parameter).ConfigureAwait(false).GetAwaiter().OnCompleted(() => Status = AsyncNotificationStatus.Complete); } catch (TaskCanceledException) { Status = AsyncNotificationStatus.Canceled; } catch { Status = AsyncNotificationStatus.Error; throw; } }
/// <summary> /// Monitor the status of the command task /// </summary> /// <param name="commandTask">Commands task to complete</param> public async void MonitorTask(Task commandTask) { //Setup monitor _commandTask = commandTask; _isMonitoring = true; //Setup cancellation token _tokenSource = new CancellationTokenSource(); _token = _tokenSource.Token; //Mark status as busy before start Status = AsyncNotificationStatus.Busy; //Start the task await Task.Factory.StartNew(() => { while (_isMonitoring) { if (_commandTask.Status == TaskStatus.RanToCompletion) { Status = AsyncNotificationStatus.Complete; _isMonitoring = false; break; } else if (_commandTask.Status == TaskStatus.Faulted) { Status = AsyncNotificationStatus.Error; _isMonitoring = false; break; } else if (_tokenSource.IsCancellationRequested) { Status = AsyncNotificationStatus.Canceled; _isMonitoring = false; break; } else { Status = AsyncNotificationStatus.Busy; } } }, _token); }
public void Update() { Status = _observableTask.Status; }