/// <summary> /// 使用指定参数来启动任务 /// </summary> /// <param name="arguments"></param> public void RunWorkASync(object arguments) { if (IsBusy || _operation != null) { return; } _operation = AsyncOperationManager.CreateOperation(null); var ts = new Thread(RunWorkAsyncInternal) { IsBackground = true, Priority = WorkerPriority }; _runworkEventArgs = new RunworkEventArgs(this) { Argument = arguments, Thread = ts }; if (UseWaitCursor) { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; } ts.Start(_runworkEventArgs); }
/// <summary> /// 引发 <see cref="DoWork"/> 事件 /// </summary> /// <param name="e">类型为 <see cref="RunworkEventArgs"/> 的事件参数</param> protected virtual void OnDoWork(RunworkEventArgs e) { var handler = DoWork; if (handler != null) { handler(this, e); } }
/// <summary> /// 引发 <see cref="WorkerThreadAborted"/> 事件 /// </summary> /// <param name="e">类型为 <see cref="RunworkEventArgs"/> 的事件参数</param> protected virtual void OnWorkerThreadAborted(RunworkEventArgs e) { if (!IsBusy) { return; } var handler = WorkerThreadAborted; if (handler == null) { return; } _operation.Post(_ => handler(this, e), null); }
/// <summary> /// 引发 <see cref="WorkCancelled"/> 事件 /// </summary> /// <param name="e">类型为 <see cref="RunworkEventArgs"/> 的事件参数</param> protected void OnWorkCancelled(RunworkEventArgs e) { if (!IsBusy) { return; } var operation = _operation; _operation = null; var handler = WorkCancelled; if (handler == null) { return; } operation.PostOperationCompleted(_ => handler(this, e), null); }