/// <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) { if (this.DoWork == null) { return; } DoWork(this, e); }
/// <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="WorkCancelled"/> 事件 /// </summary> /// <param name="e">类型为 <see cref="RunworkEventArgs"/> 的事件参数</param> protected void OnWorkCancelled(RunworkEventArgs e) { if (this.WorkCancelled == null) { return; } if (this._operation == null) { WorkCancelled(this, e); } else { this._operation.PostOperationCompleted(callWorkCancelled, e); } }
/// <summary> /// 引发 <see cref="WorkerThreadAborted"/> 事件 /// </summary> /// <param name="e">类型为 <see cref="RunworkEventArgs"/> 的事件参数</param> protected virtual void OnWorkerThreadAborted(RunworkEventArgs e) { if (WorkerThreadAborted == null) { return; } if (_operation == null) { WorkerThreadAborted(this, e); } else { _operation.Post(callWorkerThreadAborted, 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 handler = WorkCancelled; if (handler == null) { return; } _operation.PostOperationCompleted(_ => handler(this, e), null); _operation = null; }