protected override void DoProcess(ProcessItemHandler handler) { if (!this.UsePipes) { this.ProcessExtract(handler); } else { WaitHandle[] waitHandles = new WaitHandle[] { this.FinishedEvent, this.BeforeExtractItem }; ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcessExtract)); AsyncProcessWcxItemEventArgs e = new AsyncProcessWcxItemEventArgs(this); while (true) { if (WaitHandle.WaitAny(waitHandles) == 0) { break; } this.ExtractNeeded = false; handler(e); if (e.Cancel) { this.TerminateEvent.Set(); } else if (!this.ExtractNeeded) { this.ConfirmExtractItem.Set(); } } } if (this.ProcessResult != 0) { WcxErrors.ThrowExceptionForError(this.ProcessResult); } }
/// <summary> /// Synchronous processing /// </summary> public static void Example1() { var ps = PowerShell.Create().AddCommand("Get-Process"); IAsyncResult pipeAsyncResult = ps.BeginInvoke(); foreach (PSObject result in ps.EndInvoke(pipeAsyncResult)) { ProcessItemHandler?.Invoke(new ProcessItem() { Value = $"{result.Members["ProcessName"].Value,-20}{result.Members["Id"].Value}" }); } }
/// <summary> /// Asynchronous processing /// </summary> /// <returns></returns> public static async Task Example2Task() { await Task.Run(async() => { await Task.Delay(0); var ps = PowerShell.Create().AddCommand("Get-Process"); var pipeAsyncResult = ps.BeginInvoke(); foreach (var result in ps.EndInvoke(pipeAsyncResult)) { ProcessItemHandler?.Invoke(new ProcessItem() { Value = $"{result.Members["ProcessName"].Value,-20}{result.Members["Id"].Value}" }); } }); }
public void Process(ProcessItemHandler handler) { switch (this.State) { case ProcessorState.InProcess: throw new InvalidOperationException("Processor already in process"); case ProcessorState.Finished: throw new InvalidOperationException("Cannot start one processor twice"); } this.FState = ProcessorState.InProcess; try { this.DoProcess(handler); } finally { this.FState = ProcessorState.Finished; } }
/// <summary> /// Creates a processing queue /// </summary> /// <param name="threads">number of threads to fire delegates on to process items</param> /// <param name="itemHandler">delegate to call</param> /// <param name="name">name to assign to the threads</param> /// <param name="supportInjection">Whether or not the queue should be created to support injection onto a high priority list which gets processed ahead of the main list</param> public BackgroundProcessor(int threads, ProcessItemHandler itemHandler, string name) { if (threads <= 0) { throw new ArgumentException("threads cannot be <= zero", "threads"); } if (itemHandler == null) { throw new ArgumentNullException("itemHandler"); } this.handler = itemHandler; for (int i = 0; i < threads; ++i) { Thread t = new Thread(new ThreadStart(WorkerThread)); t.Name = name + "-Worker"; t.IsBackground = true; t.Priority = ThreadPriority.Lowest; t.Start(); } }
protected override void DoProcess(ProcessItemHandler handler) { if (!base.Context.FormatInfo.CanDeleteFiles) { throw new NotSupportedException(); } List<string> files = new List<string>(base.Items.Values.Count); foreach (WcxArchiveItem item in base.Items.Values) { files.Add(item.Name); if (handler != null) { handler(new SimpleProcessItemEventArgs(item, base.GetUserState(item))); } } int errorCode = base.Context.FormatInfo.DeleteFiles(base.Context.ArchiveName, files); if (errorCode != 0) { WcxErrors.ThrowExceptionForError(errorCode); } LocalFileSystemCreator.RaiseFileChangedEvent(WatcherChangeTypes.Changed, base.Context.ArchiveName); }
protected abstract void DoProcess(ProcessItemHandler handler);