/// <summary> /// 线程池任务 /// </summary> private unsafe void onTimer() { TimerTaskInfo taskInfo = new TimerTaskInfo(); Monitor.Enter(taskLock); try { do { try { while (taskHeap.Count != 0) { int index = taskHeap.Heap.Int[1]; long ticks = taskHeap.Array[index].Key; if (ticks <= Date.NowTime.Set().Ticks) { taskInfo = taskHeap.Array[index].Value; taskHeap.RemoveTop(); switch (taskInfo.ThreadType) { case TimerTaskThreadType.ThreadPool: taskInfo.Start(ThreadPool.TinyBackground); break; case TimerTaskThreadType.Queue: taskInfo.Call(); break; case TimerTaskThreadType.OnTimer: goto ONTIMER; } } else { nearTime = ticks; return; } } nearTime = long.MaxValue; return; } catch (Exception error) { AutoCSer.Log.Pub.Log.Add(Log.LogType.Error, error); } }while (true); ONTIMER :; } finally { Monitor.Exit(taskLock); System.Threading.Interlocked.Exchange(ref isTimer, 0); } try { taskInfo.Call(); } catch (Exception error) { AutoCSer.Log.Pub.Log.Add(Log.LogType.Error, error); } }
/// <summary> /// 添加新任务 /// </summary> /// <param name="value">任务委托</param> /// <param name="type">调用类型</param> /// <param name="threadType">定时任务线程模式</param> /// <param name="runTime">执行时间</param> private void add(object value, Thread.CallType type, TimerTaskThreadType threadType, DateTime runTime) { long runTimeTicks = runTime.Ticks; TimerTaskInfo taskInfo = new TimerTaskInfo { Value = value, CallType = type, ThreadType = threadType }; Monitor.Enter(taskLock); try { taskHeap.Push(runTimeTicks, ref taskInfo); if (runTimeTicks < nearTime) { nearTime = runTimeTicks; } } finally { Monitor.Exit(taskLock); } }