コード例 #1
0
        internal TaskWrapper getTask()
        {
            lock (TaskQueue) //使用TryEnter 导致了cpu的占用不稳定,升高,恢复原来的代码
            //if (Monitor.TryEnter(TaskQueue))
            {
                try
                {
                    if (TaskQueue.Count <= 0)
                    {
                        return(null);
                    }
                    else
                    {
                        TaskWrapper currentTask = TaskQueue.First.Value;
                        TaskQueue.RemoveFirst();
                        if (currentTask.canExecute)
                        {
                            return(currentTask);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
                catch (System.Exception /* ex*/)
                {
                }
                //finally
                //{
                //    Monitor.Exit(TaskQueue);
                //}
            }
            //else
            //{
            //    return null;
            //}

            return(null);
        }
コード例 #2
0
        internal TaskWrapper GetPreiodictTask(long ticks)
        {
            TaskWrapper result;

            lock (this.PreiodictTaskList)
            {
                if (this.PreiodictTaskList.Count == 0)
                {
                    result = null;
                }
                else if (this.PreiodictTaskList[0].StartTime > ticks)
                {
                    result = null;
                }
                else
                {
                    TaskWrapper taskWrapper = this.PreiodictTaskList[0];
                    this.PreiodictTaskList.RemoveAt(0);
                    result = taskWrapper;
                }
            }
            return(result);
        }
コード例 #3
0
 internal TaskWrapper getTask()
 {
     lock (this.TaskQueue)
     {
         try
         {
             if (this.TaskQueue.Count <= 0)
             {
                 return(null);
             }
             TaskWrapper currentTask = this.TaskQueue.First.Value;
             this.TaskQueue.RemoveFirst();
             if (currentTask.canExecute)
             {
                 return(currentTask);
             }
             return(null);
         }
         catch (Exception)
         {
         }
     }
     return(null);
 }
コード例 #4
0
 public PeriodicTaskHandleImpl(TaskWrapper taskWrapper, ScheduleExecutor executor)
 {
     this.taskWrapper = taskWrapper;
     this.executor    = executor;
 }
コード例 #5
0
        public void work()
        {
            lock (_lock)
            {
                nThreadCount++;
                nThreadOrder = nThreadCount;
            }

            TaskWrapper taskWrapper = null;

            int lastTickCount = int.MinValue;

            while (!Program.NeedExitServer)
            {
                //检索可执行的任务
                int tickCount = Environment.TickCount;
                if (tickCount <= lastTickCount + 5)
                {
                    if (lastTickCount <= 0 || tickCount >= 0) //考虑当打到int最大值时的情况
                    {
                        Thread.Sleep(5);
                        continue;
                    }
                }
                lastTickCount = tickCount;

                long ticks = TimeUtil.NOW();
                while (true)
                {
                    try
                    {
                        taskWrapper = getCanExecuteTask(ticks);
                        if (null == taskWrapper || null == taskWrapper.CurrentTask)
                        {
                            break;
                        }

                        if (taskWrapper.canExecute)
                        {
                            try
                            {
                                taskWrapper.CurrentTask.run();
                            }
                            catch (System.Exception ex)
                            {
                                DataHelper.WriteFormatExceptionLog(ex, "异步调度任务执行异常", false);
                            }
                        }

                        //如果是周期执行的任务
                        if (taskWrapper.Periodic > 0 && taskWrapper.canExecute)
                        {
                            //设置下一次执行的时间
                            taskWrapper.resetStartTime();
                            executor.addTask(taskWrapper);
                            taskWrapper.addExecuteCount();
                        }
                    }
                    catch (System.Exception /* ex*/)
                    {
                        //LogManager.WriteLog(LogTypes.Error, string.Format("异步调度任务执行错误: {0}", ex));
                    }
                }
            }

            System.Console.WriteLine(string.Format("ScheduleTask Worker{0}退出...", nThreadOrder));
        }