示例#1
0
        /// <summary>
        /// Init the multithread run time resource
        /// </summary>
        internal void InitMutltiThreadResources()
        {
            taskScheduler                  = new LimitedConcurrencyTaskScheduler(GetCmdletConcurrency(), CmdletCancellationToken);
            OutputStream                   = new TaskOutputStream(CmdletCancellationToken);
            OutputStream.OutputWriter      = WriteObject;
            OutputStream.ErrorWriter       = WriteExceptionError;
            OutputStream.ProgressWriter    = WriteProgress;
            OutputStream.VerboseWriter     = WriteVerbose;
            OutputStream.DebugWriter       = WriteDebugWithTimestamp;
            OutputStream.ConfirmWriter     = ShouldProcess;
            OutputStream.TaskStatusQueryer = taskScheduler.IsTaskCompleted;
            taskScheduler.OnError         += TaskErrorHandler;

            int    summaryRecordId = 0;
            string summary         = String.Format(Resources.TransmitActiveSummary, taskScheduler.TotalTaskCount,
                                                   taskScheduler.FinishedTaskCount, taskScheduler.FailedTaskCount, taskScheduler.ActiveTaskCount);
            string activity = string.Format(Resources.TransmitActivity, this.MyInvocation.MyCommand);

            summaryRecord = new ProgressRecord(summaryRecordId, activity, summary);
            CmdletCancellationToken.Register(() => OutputStream.CancelConfirmRequest());
        }
 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().ConfigureAwait(false);

                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;
            }
        }