Exemple #1
0
 public new Task <bool> WriteDebug(string text)
 {
     return(QueueMessage(() => {
         try {
             base.WriteDebug(text);
         } catch (PipelineStoppedException pipelineStoppedException) {
             // this can throw if the pipeline is stopped
             // but that's ok, because it just means
             // that we're done.
             Cancel();
             _pipelineStopped = pipelineStoppedException;
         } catch {
             // any other means that we're done anyway too.
         }
     }));
 }
Exemple #2
0
 internal void Push(PipelineProcessor item)
 {
     if (item == null)
     {
         throw PSTraceSource.NewArgumentNullException("item");
     }
     lock (this._syncRoot)
     {
         if (this._stopping)
         {
             PipelineStoppedException exception = new PipelineStoppedException();
             throw exception;
         }
         this._stack.Push(item);
     }
     item.LocalPipeline = this._localPipeline;
 }
Exemple #3
0
        private void AsyncRun(Func <bool> asyncAction)
        {
            try {
                using (_messages = new BlockingCollection <TaskCompletionSource <bool> >()) {
                    // spawn the activity off in another thread.
                    Task.Factory.StartNew(() => {
                        try {
                            if (!IsInitialized)
                            {
                                Init();
                            }

                            return(asyncAction());
                        } catch (Exception e) {
                            Error("MSG:UnhandledException", ErrorCategory.InvalidOperation.ToString(), e.Message, "{0}/{1}/{2}", e.GetType().Name, e.Message, e.StackTrace);
                        } finally {
                            // when the task is done, mark the msg queue as complete
                            if (_messages != null)
                            {
                                _messages.CompleteAdding();
                            }
                        }
                        return(false);
                    }, TaskCreationOptions.LongRunning);


                    // process the queue of messages back in the main thread so that they
                    // can properly access the non-thread-safe-things in cmdlet
                    foreach (var message in _messages)
                    {
                        InvokeMessage(message);
                    }
                }

                if (_pipelineStopped != null)
                {
                    throw _pipelineStopped;
                }
            } finally {
                _messages        = null;
                _pipelineStopped = null;
            }
        }
Exemple #4
0
 public new Task <bool> WriteObject(object sendToPipeline, bool enumerateCollection)
 {
     return(QueueMessage(() => {
         if (!IsCanceled)
         {
             try {
                 base.WriteObject(sendToPipeline, enumerateCollection);
             } catch (PipelineStoppedException pipelineStoppedException) {
                 // this can throw if the pipeline is stopped
                 // but that's ok, because it just means
                 // that we're done.
                 Cancel();
                 _pipelineStopped = pipelineStoppedException;
             } catch {
                 // any other means that we're done anyway too.
             }
         }
     }));
 }
Exemple #5
0
 public new Task <bool> WriteVerbose(string text)
 {
     if (!IsInvocation)
     {
         return(false.AsResultTask());
     }
     return(QueueMessage(() => {
         try {
             // if we're stopping, skip this call anyway.
             base.WriteVerbose(text);
         } catch (PipelineStoppedException pipelineStoppedException) {
             // this can throw if the pipeline is stopped
             // but that's ok, because it just means
             // that we're done.
             Cancel();
             _pipelineStopped = pipelineStoppedException;
         } catch {
             // any other means that we're done anyway too.
         }
     }));
 }
Exemple #6
0
        private void InvokeMessage(TaskCompletionSource <bool> message)
        {
            var func = message.Task.AsyncState as Func <bool>;

            if (func != null)
            {
                try {
                    message.SetResult(func());
                } catch (PipelineStoppedException pipelineStoppedException) {
                    _pipelineStopped = pipelineStoppedException;
                    Cancel();
                    message.SetException(pipelineStoppedException);
                } catch (Exception e) {
                    message.SetException(e);
                }
            }
            else
            {
                // this should have been a Func<bool>.
                // cancel it.
                message.SetCanceled();
            }
        }
Exemple #7
0
        private PipelineStoppedException NewPipelineStoppedException()
        {
            PipelineStoppedException pipelineStoppedException = new PipelineStoppedException();

            return(pipelineStoppedException);
        }
Exemple #8
0
 /// <summary>
 /// Push item in to PipelineProcessor stack
 /// </summary>
 /// <param name="item"></param>
 internal void Push(PipelineProcessor item)
 {
     if (item == null)
     {
         throw PSTraceSource.NewArgumentNullException("item");
     }
     lock (_syncRoot)
     {
         if (_stopping)
         {
             PipelineStoppedException e = new PipelineStoppedException();
             throw e;
         }
         _stack.Push(item);
     }
     item.LocalPipeline = _localPipeline;
 }
 private void WriteJobResults(bool nonblocking)
 {
     if (this.job != null)
     {
         PipelineStoppedException exception = null;
         this.job.PropagateThrows = this.propagateErrors;
         do
         {
             if (!nonblocking)
             {
                 if (this.disconnectComplete != null)
                 {
                     WaitHandle.WaitAny(new WaitHandle[] { this.disconnectComplete, this.job.Results.WaitHandle });
                 }
                 else
                 {
                     this.job.Results.WaitHandle.WaitOne();
                 }
             }
             try
             {
                 this.WriteStreamObjectsFromCollection(this.job.ReadAll());
             }
             catch (PipelineStoppedException exception2)
             {
                 exception = exception2;
             }
         }while (!nonblocking && !this.job.IsTerminalState());
         try
         {
             this.WriteStreamObjectsFromCollection(this.job.ReadAll());
         }
         catch (PipelineStoppedException exception3)
         {
             exception = exception3;
         }
         if (exception != null)
         {
             this.HandlePipelinesStopped();
             throw exception;
         }
         if (this.job.JobStateInfo.State == JobState.Disconnected)
         {
             if ((base.ParameterSetName == "Session") || (base.ParameterSetName == "FilePathRunspace"))
             {
                 PSRemotingJob item = this.job.CreateDisconnectedRemotingJob();
                 if (item != null)
                 {
                     item.PSJobTypeName = RemoteJobType;
                     base.JobRepository.Add(item);
                     this.asjob = true;
                     foreach (Job job2 in item.ChildJobs)
                     {
                         PSRemotingChildJob job3 = job2 as PSRemotingChildJob;
                         if (job3 != null)
                         {
                             PSSession pSSession = this.GetPSSession(job3.Runspace.InstanceId);
                             if (pSSession != null)
                             {
                                 this.WriteNetworkFailedError(pSSession);
                                 base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSession, new object[] { pSSession.Name, pSSession.InstanceId, pSSession.ComputerName }));
                             }
                         }
                     }
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectedJob, item.Name));
                 }
             }
             else if ((base.ParameterSetName == "ComputerName") || (base.ParameterSetName == "FilePathComputerName"))
             {
                 foreach (PSSession session2 in this.GetDisconnectedSessions(this.job))
                 {
                     base.RunspaceRepository.AddOrReplace(session2);
                     this.WriteNetworkFailedError(session2);
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSession, new object[] { session2.Name, session2.InstanceId, session2.ComputerName }));
                     base.WriteWarning(StringUtil.Format(RemotingErrorIdStrings.RCDisconnectSessionCreated, session2.Name, session2.InstanceId));
                 }
             }
             this.HandleThrottleComplete(null, null);
         }
     }
 }