static private void HandleTransferException(Exception e, ProgressRecord record, TaskOutputStream outputStream) { if (record != null) { record.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.TransmitFailed, e.Message); outputStream.WriteProgress(record); } }
static public async Task DoTransfer(Func<Task> doTransfer, ProgressRecord record, TaskOutputStream outputStream) { try { await doTransfer(); if (record != null) { record.PercentComplete = 100; record.StatusDescription = Resources.TransmitSuccessfully; outputStream.WriteProgress(record); } } catch (OperationCanceledException) { if (record != null) { record.StatusDescription = Resources.TransmitCancelled; outputStream.WriteProgress(record); } } catch (TransferException e) { // DMLib wrappers StorageException in its InnerException but didn't expose any detailed error messages, // here throw its inner exception out to show more readable error messages. StorageException se = e.InnerException as StorageException; if (null != se) { HandleTransferException(se, record, outputStream); throw se; } else { HandleTransferException(e, record, outputStream); throw; } } catch (Exception e) { HandleTransferException(e, record, outputStream); throw; } }
/// <summary> /// Init the multithread run time resource /// </summary> internal void InitMutltiThreadResources() { _taskScheduler = new LimitedConcurrencyTaskScheduler(GetCmdletConcurrency(), CmdletCancellationToken); OutputStream = new TaskOutputStream(CmdletCancellationToken) { OutputWriter = WriteObject, ErrorWriter = WriteExceptionError, ProgressWriter = WriteProgress, VerboseWriter = WriteVerbose, DebugWriter = WriteDebugWithTimestamp, ConfirmWriter = ShouldProcess, TaskStatusQueryer = _taskScheduler.IsTaskCompleted }; _taskScheduler.OnError += TaskErrorHandler; const int summaryRecordId = 0; var summary = String.Format(Resources.TransmitActiveSummary, _taskScheduler.TotalTaskCount, _taskScheduler.FinishedTaskCount, _taskScheduler.FailedTaskCount, _taskScheduler.ActiveTaskCount); var activity = string.Format(Resources.TransmitActivity, MyInvocation.MyCommand); summaryRecord = new ProgressRecord(summaryRecordId, activity, summary); CmdletCancellationToken.Register(() => OutputStream.CancelConfirmRequest()); }