public void OnDoWork(MultiDoWorkEventArgs e) { if (DoWork != null) { DoWork(this, e); } }
void multiBackgroundWorker1_DoWork(object sender, Core.MultiDoWorkEventArgs e) { //e.ta string taskId = e.TaskID.ToString(); e.Result = Compute(taskId, (MultiBackgroundWorker)sender, e); }
private object Compute(string taskId, MultiBackgroundWorker multiBackgroundWorker, MultiDoWorkEventArgs e) { long result = 0; int n = 200; for (int i = 1; i <= n; i++) { //应该判断任务是否已被移除或取消 if (multiBackgroundWorker.WhetherTaskCancelledOrNot(taskId)) { e.Cancel = true; break; } result += i; Thread.Sleep(100); int progressPercentage = (int)((float)i / (float)n * 100); multiBackgroundWorker.ReportProgress(taskId, progressPercentage, Thread.CurrentThread.ManagedThreadId); } return result; }
public void WorkThreadStart(AsyncOperation async, object argument) { AsyncOperation asyncOp = async; bool cancelled = false; Exception error = null; object result = null; try { MultiDoWorkEventArgs args = new MultiDoWorkEventArgs(asyncOp.UserSuppliedState, argument); this.OnDoWork(args); if (args.Cancel) { cancelled = true; } else { result = args.Result; } } catch (Exception exception) { error = exception; } if (!cancelled) { lock (this.userStateToLifeTime.SyncRoot) { this.userStateToLifeTime.Remove(asyncOp.UserSuppliedState); } } MultiRunWorkerCompletedEventArgs completeArgs = new MultiRunWorkerCompletedEventArgs(asyncOp.UserSuppliedState, result, error, cancelled); asyncOp.PostOperationCompleted(onRunWorkerCompletedDelegate, completeArgs); }