/// <summary> /// 停止此调度器并等待其状态到达,如果在指定的时间内未到达停止状态,则引发System.TimeoutException /// </summary> /// <exception cref="System.TimeoutException" >未能在指定时间内完成停止</exception> /// <exception cref="System.InvalidOperationException">Stop在当前状态不可用</exception> /// <param name="waitTimeout">毫秒 -1时不等待,0时无限等待</param> public void Stop(int waitTimeout) { if (StatusFlag == -1) { throw new InvalidOperationException("JobScheduler已释放"); } else if (StatusFlag == 2) { throw new InvalidOperationException("正在停止中 请勿重复调用"); } System.Threading.Interlocked.Exchange(ref StatusFlag, 0); AddJobWaitHandle.Set(); if (waitTimeout != -1) { System.Diagnostics.Debug.WriteLine("等待Job执行线程退出"); if (waitTimeout == 0) { LoopWaitHandle.WaitOne(); } else if (LoopWaitHandle.WaitOne(waitTimeout, false)) { throw new TimeoutException( String.Format("Job执行线程未能在指定时间内({0})结束", waitTimeout.ToString())); } } }
public void AddJob(TJob job) { JobQueue.Enqueue(job); AddJobWaitHandle.Set(); }