public static BaseTask Add(Action callback, DateTime time) { FuturedTask task = new FuturedTask(callback, time); Add(task); return(task); }
private static void RunTask(object task) { //TODO: Try/catch here for exception logging or similar things FuturedTask taskToRun = (FuturedTask)task; taskToRun.Start(); }
private static void Add(FuturedTask task) { long tillTask = (task.scheduledTime - DateTime.UtcNow).Ticks; if (tillTask < NowLimit) { AddToImmediate(task); } else //Could have another else if here for putting stuff into the future set right away, but that should be uncommon enough that it doesn't need to be checked here. { long rawSetIndex = (task.scheduledTime.Ticks - StartOffset) / TimeSpan.TicksPerMillisecond / MillisecondsPerSet; int setIndex = (int)rawSetIndex; int setToAddTo = setIndex % NumberOfSets; //int setOffset = (int)(setIndex) / NumberOfSets; ? Don't *really* need this. int success = TickSets[setToAddTo].Add(task, setIndex); if (success < 0) { AddToImmediate(task); } else if (success > 0) { //TODO: add to 'future set' instead? Add to now works okay but isn't ideal if there are many long-term waits. AddToImmediate(task); } } }
private static void AddToImmediate(FuturedTask task) { ImmediateTickSet.Add(task); if (task.scheduledTime < NextWakeup) { MaySleep.Set(); } }
/// <summary> /// Try to add the task to this queue. /// </summary> /// <param name="task"></param> /// <param name="expectedOffset"></param> /// <returns>negative if the task is too soon, positive if the task is too far in the future. 0 if the task has been added to this queue.</returns> public int Add(FuturedTask task, int expectedOffset) { if (switching) { int diff = scheduleOffset - expectedOffset; return(diff == 0 ? -1 : diff); } else { busy++; int diff = scheduleOffset - expectedOffset; if (diff == 0) { set.Add(task); } busy--; return(diff); } }
private static void RunOrQueue(FuturedTask task) { if (task.IsCanceled()) { return; } if ((task.scheduledTime - DateTime.UtcNow).TotalMilliseconds < NowLimit && !ThreadManager.MUDIsPaused) { ThreadPool.QueueUserWorkItem(RunTask, task); } else { int index = TasksToRunSoon.BinarySearch(task); if (index < 0) { index = ~index; } TasksToRunSoon.Insert(index, task); } }