public void Abort() { if (this._workerThread == null) { throw new NullReferenceException("Task委托不能为空!"); } if (this._currentStatue != MyProcessBarStatue.Aborted) { //若之前为Suspended状态 则需要先Resume才可以终止工作线程 this.Resume(); _workerThread.Abort(); this._currentStatue = MyProcessBarStatue.Aborted; } }
public void Run() { if (this._workerThread == null) { throw new NullReferenceException("Task委托不能为空!"); } //初次启动 需要同时启动工作线程和监听线程 if (this._currentStatue == MyProcessBarStatue.UnStarted) { _monitorThread.IsBackground = true; _monitorThread.Start(); _workerThread.IsBackground = true; _workerThread.Start(); this._currentStatue = MyProcessBarStatue.Running; } //被终止后第二次启动 只需要重启工作线程就可以 else if (this._currentStatue == MyProcessBarStatue.Aborted) { this._currentStatue = MyProcessBarStatue.Running; //若原工作线程已经终止 则重新初始化工作线程 if (this._workerThread.ThreadState == ThreadState.Aborted) { this._workerThread = null; this._workerThread = new Thread(() => _task(ref _percentage)); } _workerThread.IsBackground = true; _workerThread.Start(); } else { throw new InvalidOperationException("已经开始的任务无法再次开始!"); } }
public void Stop() { if (_workerThread == null) { throw new NullReferenceException("Task委托不能为空!"); } if (this._currentStatue == MyProcessBarStatue.Aborted) { throw new InvalidOperationException("已经终止的操作无法暂停!"); } if (this._currentStatue != MyProcessBarStatue.Suspended) { _workerThread.Suspend(); _monitorThread.Suspend(); this._currentStatue = MyProcessBarStatue.Suspended; } }
public void Resume() { if (_workerThread == null) { throw new NullReferenceException("Task委托不能为空!"); } if (this._currentStatue == MyProcessBarStatue.Aborted) { throw new InvalidOperationException("已经终止的操作无法继续!"); } if (this._currentStatue == MyProcessBarStatue.Suspended) { _monitorThread.Resume(); _workerThread.Resume(); this._currentStatue = MyProcessBarStatue.Running; } }