Example #1
0
 /// <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()));
         }
     }
 }
Example #2
0
        void Loop(object nothing)
        {
            PreJobLoop();
            LoopWaitHandle.Reset();
            System.Threading.Interlocked.CompareExchange(ref StatusFlag, 1, 0);
            try
            {
                do
                {
                    if (JobQueue.Count > 0)
                    {
                        TJob job;
                        bool got = JobQueue.TryDequeue(out job);
                        if (got)
                        {
                            if (!CancelBits[job.EnableBit])
                            {
                                try
                                {
                                    ExecuteJobAction(job);
                                }
                                catch (Exception e)
                                {
                                    System.Diagnostics.Debug.WriteLine(String.Format("Job执行循环中发生未处理的异常"));
                                    System.Diagnostics.Debug.WriteLine(e.GetType() + " " + e.Message);
                                    System.Diagnostics.Debug.WriteLine(e.StackTrace);
#if DEBUG
                                    throw e;
#endif
                                }
                            }
                        }
                    }
                    else
                    {
                        AddJobWaitHandle.WaitOne();
                    }
                } while (StatusFlag == 1);
            }
            finally
            {
                System.Diagnostics.Debug.WriteLine("等待Job执行线程上的循环已结束");
                PostJobLoop();
                LoopWaitHandle.Set();
                System.Diagnostics.Debug.WriteLine("Job执行线程已退出");
            }
        }
Example #3
0
 public void AddJob(TJob job)
 {
     JobQueue.Enqueue(job);
     AddJobWaitHandle.Set();
 }