public Task<AggregateOperationsResult> ProcessQueue(IEnumerable<IOperationViewModel> operations, CancellationToken token) { foreach (var operation in operations) { // Check if previous previousOperation failed. var previousOperation = GetPreviousItem(operations, operation); if (token.IsCancellationRequested || !this.CanContinue(previousOperation)) { break; } operation.State = OperationState.InProgress; eventAggregator.PublishOnUIThread(new LogEntry(operation.Description, LogEntrySeverity.Info, LogEntrySource.UI)); var result = new OperationResult(); try { token.ThrowIfCancellationRequested(); result = operation.Process(); LogResultMessages(result); resultHandlershandle[result.State](result, operation); } catch (OperationCanceledException) { resultHandlershandle[OperationResultState.Canceled](result, operation); } catch (Exception e) { eventAggregator.PublishOnUIThread( new LogEntry(string.Format("Unhandled exception occurred: {0}", e.Message), LogEntrySeverity.Error, LogEntrySource.UI)); } } return Task.FromResult(CalculateResult(operations)); }
private void LogResultMessages(OperationResult result) { const int maxEntriesToShow = 1500; if (result.LogEntries.Count <= maxEntriesToShow) { this.PublishLogEntries(result.LogEntries); } else { var lastEntries = result.LogEntries.AsEnumerable<LogEntry>().Skip(Math.Max(0, result.LogEntries.Count - maxEntriesToShow)); this.PublishLogEntries(lastEntries); eventAggregator.PublishOnUIThread(new LogEntry("The converter returned " + result.LogEntries.Count + " lines.", LogEntrySeverity.Warning, LogEntrySource.UI)); eventAggregator.PublishOnUIThread(new LogEntry("Only showing the last " + maxEntriesToShow, LogEntrySeverity.Warning, LogEntrySource.UI)); } }
private void HandleWarning(OperationResult result, IOperationViewModel operation) { operation.State = OperationState.CompleteWithWarnings; eventAggregator.PublishOnUIThread( new LogEntry(string.Format("{0} completed with warnings!", operation.Description), LogEntrySeverity.Warning, LogEntrySource.UI)); }
private void HandleSuccess(OperationResult result, IOperationViewModel operation) { operation.State = OperationState.Success; eventAggregator.PublishOnUIThread( new LogEntry(string.Format("{0} completed successfully.", operation.Description), LogEntrySeverity.Info, LogEntrySource.UI)); }
private void HandleErrors(OperationResult result, IOperationViewModel operation) { operation.State = OperationState.Failed; eventAggregator.PublishOnUIThread(new LogEntry(string.Format("{0} failed!", operation.Description), LogEntrySeverity.Error, LogEntrySource.UI)); }
private void HandleCancellation(OperationResult result, IOperationViewModel operation) { eventAggregator.PublishOnUIThread( new LogEntry(string.Format("Operation {0} canceled.", operation.Description), LogEntrySeverity.Warning, LogEntrySource.UI)); }