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.GetBlockingEnumerable(CancellationEvent.Token)) { InvokeMessage(message); } } if (_pipelineStopped != null) { throw _pipelineStopped; } } finally { _messages = null; _pipelineStopped = null; } }
public IEnumerable <T> GetBlockingEnumerable() { return(Results.GetBlockingEnumerable(_cancellationTokenSource.Token)); }
public IEnumerator <object> GetEnumerator() { return(_output.GetBlockingEnumerable().GetEnumerator()); }