public void work() { lock (Worker._lock) { Worker.nThreadCount++; this.nThreadOrder = Worker.nThreadCount; } TaskWrapper taskWrapper = null; int lastTickCount = int.MinValue; while (!Program.NeedExitServer) { int tickCount = Environment.TickCount; if (tickCount <= lastTickCount + 5) { if (lastTickCount <= 0 || tickCount >= 0) { Thread.Sleep(5); continue; } } lastTickCount = tickCount; long ticks = TimeUtil.NOW(); for (;;) { try { taskWrapper = this.getCanExecuteTask(ticks); if (taskWrapper == null || null == taskWrapper.CurrentTask) { break; } if (taskWrapper.canExecute) { try { taskWrapper.CurrentTask.run(); } catch (Exception ex) { DataHelper.WriteFormatExceptionLog(ex, "异步调度任务执行异常", false, false); } } if (taskWrapper.Periodic > 0L && taskWrapper.canExecute) { taskWrapper.resetStartTime(); this.executor.addTask(taskWrapper); taskWrapper.addExecuteCount(); } } catch (Exception) { } } } SysConOut.WriteLine(string.Format("ScheduleTask Worker{0}退出...", this.nThreadOrder)); }
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)); }