// 工作线程每一轮循环的实质性工作 public override void Worker() { try { PropertyTask task = null; int nRestCount = 0; // 取出第一个元素 if (this.m_lock.TryEnterWriteLock(m_nLockTimeout) == false) { throw new LockException("锁定尝试中超时"); } try { if (this._tasks.Count == 0) { return; } task = this._tasks[0]; this._tasks.RemoveAt(0); nRestCount = this._tasks.Count; } finally { this.m_lock.ExitWriteLock(); } try { lock (syncRoot) { this._currentTask = task; } if (task.LoadData() == false) { return; } task.ShowData(); lock (syncRoot) { this._currentTask = null; } } finally { if (nRestCount > 0) { this.Activate(); } } } catch (Exception ex) { Program.MainForm.WriteErrorLog("PropertyTaskList Worker() 出现异常: " + ExceptionUtil.GetDebugText(ex)); Program.MainForm.ReportError("dp2circulation 调试信息", "PropertyTaskList Worker() 出现异常: " + ExceptionUtil.GetDebugText(ex)); } }
// 加入一个任务到列表中 // parameters: // bClearBefore 清除以前积累的任务。也要中断插入前正在做的任务 public void AddTask(PropertyTask task, bool bClearBefore) { if (this.m_lock.TryEnterWriteLock(500) == false) // m_nLockTimeout { throw new LockException("锁定尝试中超时"); } try { if (bClearBefore) { this._tasks.Clear(); lock (syncRoot) { if (_currentTask != null) { Task.Factory.StartNew(() => CancelTask(_currentTask)); } } } task.Container = this; this._tasks.Add(task); } finally { this.m_lock.ExitWriteLock(); } // 触发任务开始执行 this.Activate(); }
void CancelTask(PropertyTask task) { if (task != null) { task.Cancel(); } }
// 工作线程每一轮循环的实质性工作 public override void Worker() { try { PropertyTask task = null; int nRestCount = 0; // 取出第一个元素 if (this.m_lock.TryEnterWriteLock(m_nLockTimeout) == false) throw new LockException("锁定尝试中超时"); try { if (this._tasks.Count == 0) return; task = this._tasks[0]; this._tasks.RemoveAt(0); nRestCount = this._tasks.Count; } finally { this.m_lock.ExitWriteLock(); } try { lock (syncRoot) { this._currentTask = task; } if (task.LoadData() == false) return; task.ShowData(); lock (syncRoot) { this._currentTask = null; } } finally { if (nRestCount > 0) this.Activate(); } } catch (Exception ex) { this.MainForm.WriteErrorLog("PropertyTaskList Worker() 出现异常: " + ExceptionUtil.GetDebugText(ex)); this.MainForm.ReportError("dp2circulation 调试信息", "PropertyTaskList Worker() 出现异常: " + ExceptionUtil.GetDebugText(ex)); } }
// 加入一个任务到列表中 // parameters: // bClearBefore 清除以前积累的任务。也要中断插入前正在做的任务 public void AddTask(PropertyTask task, bool bClearBefore) { if (this.m_lock.TryEnterWriteLock(500) == false) // m_nLockTimeout throw new LockException("锁定尝试中超时"); try { if (bClearBefore) { this._tasks.Clear(); lock (syncRoot) { if (_currentTask != null) Task.Factory.StartNew(() => CancelTask(_currentTask)); } } task.Container = this; this._tasks.Add(task); } finally { this.m_lock.ExitWriteLock(); } // 触发任务开始执行 this.Activate(); }
void CancelTask(PropertyTask task) { if (task != null) task.Cancel(); }