public static IObservable <T> ToObservable <T>(this PipelineReader <T> reader) { return(Observable.Create <T>(o => Observable.FromEventPattern( a => reader.DataReady += a, a => reader.DataReady -= a, Scheduler.CurrentThread) .StartWith(new EventPattern <object>(reader, EventArgs.Empty)) .Subscribe(_ => { //Debug.WriteLine($"PipeReaderThread: {Thread.CurrentThread.ManagedThreadId}"); do { foreach (var n in reader.NonBlockingRead()) { if (!AutomationNull.Value.Equals(n)) { o.OnNext(n); } else { ; //maybe multiple null inside stream are allowed } } }while (reader.Count > 0); if (reader.EndOfPipeline) { o.OnCompleted(); } } ))); }
private void HandlePipelineErrorDataReady(object sender, EventArgs e) { PipelineReader <object> reader = sender as PipelineReader <object>; if (reader != null && reader.IsOpen) { WritePipelineCollection(reader.NonBlockingRead(), PSStreamObjectType.Error); } }
private void ErrorObjectStreamHandler(object sender, EventArgs e) { PipelineReader <object> pipelineReader = (PipelineReader <object>)sender; Collection <object> objs = pipelineReader.NonBlockingRead(); foreach (object obj in objs) { this.parent.ErrorSerializer.Serialize(obj); } }
private void OutputObjectStreamHandler(object sender, EventArgs e) { PipelineReader <PSObject> pipelineReader = (PipelineReader <PSObject>)sender; Collection <PSObject> pSObjects = pipelineReader.NonBlockingRead(); foreach (PSObject pSObject in pSObjects) { this.parent.OutputSerializer.Serialize(pSObject); } }
// called on the pipeline thread private void ErrorObjectStreamHandler(object sender, EventArgs e) { // e is just an empty instance of EventArgs, so we ignore it. sender is the PipelineReader that raised it's // DataReady event that calls this handler, which is the PipelineReader for the Error object stream. PipelineReader <object> reader = (PipelineReader <object>)sender; // we use NonBlockingRead instead of Read, as Read would block if the reader has no objects. While it would be // inconsistent for this method to be called when there are no objects, since it will be called synchronously on // the pipeline thread, blocking in this call until an object is streamed would deadlock the pipeline. So we // prefer to take no chance of blocking. Collection <object> objects = reader.NonBlockingRead(); foreach (object obj in objects) { _parent.ErrorSerializer.Serialize(obj); } }
/// <summary> /// Output data arrived. /// </summary> /// <param name="sender">Contains the result as List of strings.</param> /// <param name="EventArgs"/>The instance containing the event data.</param> internal void Output_DataReady(object sender, EventArgs e) { PipelineReader <PSObject> output = sender as PipelineReader <PSObject>; List <string> lStringOutput = new List <string>(); List <object> lOutput = new List <object>(); if (output != null) { Collection <PSObject> pso = output.NonBlockingRead(); if (pso.Count > 0) { //Forward the Raw data... if (this.RawOutput != null) { this.RawOutput.Invoke(pso, e); } foreach (PSObject PO in pso) { if (PO != null) { lStringOutput.Add(PO.ToString()); foreach (string sType in PO.TypeNames) { ConvertThroughString cts = new ConvertThroughString(); Type objectType = Type.GetType(sType.Replace("Deserialized.", "")); if (cts.CanConvertFrom(PO, objectType)) { try { lOutput.Add(cts.ConvertFrom(PO, objectType, null, true)); break; } catch { try { System.Collections.Hashtable HT = new System.Collections.Hashtable(); foreach (PSPropertyInfo PI in PO.Properties) { try { HT.Add(PI.Name, PI.Value.ToString()); } catch { } } lOutput.Add(HT); break; } catch { } //break; } } else { } } } } } if (output.EndOfPipeline & output.IsOpen) { output.Close(); } } else { PipelineReader <object> error = sender as PipelineReader <object>; if (error != null) { while (error.Count > 0) { lStringOutput.Add(error.Read().ToString()); lOutput.Add(error.Read()); } if (error.EndOfPipeline) { error.Close(); } if (this.ErrorOccured != null) { this.ErrorOccured.Invoke(sender, e); } _autoResetEvent.Set(); } } //Forward output as ListOfStrings if (this.StringOutput != null) { this.StringOutput.Invoke(lStringOutput, e); } if (this.TypedOutput != null) { this.TypedOutput.Invoke(lOutput, e); } _autoResetEvent.Set(); }