private void _w_DoWork(object sender, DoWorkEventArgs e) { WorkerContext res = new WorkerContext(StatusCode.OK, null, new WorkerDelegate(_w, this)); for (_i = 0; _i < _ntasks; _i++) { _w.ReportProgress(0); res = _tasks[_i].Invoke(res); ReturnedObjects.Add((WorkerContext)res.Clone()); _w.ReportProgress(100); Thread.Sleep(500); // 0.5s sleep } }
public static PsStreamEventHandlers GetUIHandlers(WorkerContext ctx) => new PsStreamEventHandlers() { Debug = (o, e) => { DebugRecord newRecord = ((PSDataCollection <DebugRecord>)o)[e.Index]; if (!string.IsNullOrEmpty(newRecord.Message)) { Program.MainFrmInstance.SetStatus(newRecord.Message); } }, Error = (o, ev) => { ErrorRecord newRecord = ((PSDataCollection <ErrorRecord>)o)[ev.Index]; MessageBox.Show(newRecord.ToString(), "Powershell Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }, Information = (o, e) => { InformationalRecord newRecord = ((PSDataCollection <InformationalRecord>)o)[e.Index]; if (!string.IsNullOrEmpty(newRecord.Message)) { Program.MainFrmInstance.SetStatus(newRecord.Message); } }, Progress = (o, ev) => { ProgressRecord r = ((PSDataCollection <ProgressRecord>)o)[ev.Index]; ctx.d.ReportCaption(r.Activity); if (r.PercentComplete >= 0 && r.PercentComplete <= 100) { ctx.d.ReportProgress(r.PercentComplete); } }, Verbose = (o, e) => { VerboseRecord newRecord = ((PSDataCollection <VerboseRecord>)o)[e.Index]; if (!string.IsNullOrEmpty(newRecord.Message)) { Program.MainFrmInstance.SetStatus(newRecord.Message); } }, Warning = (o, e) => { WarningRecord newRecord = ((PSDataCollection <WarningRecord>)o)[e.Index]; if (!string.IsNullOrEmpty(newRecord.Message)) { Program.MainFrmInstance.SetStatus(newRecord.Message); } } };