public TaskWrapper(ScheduleTask task, long delay, long periodic) { this.currentTask = task; this.startTime = TimeUtil.NOW() + delay; this.periodic = periodic; }
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)); } } } SysConOut.WriteLine(string.Format("ScheduleTask Worker{0}退出...", nThreadOrder)); }