示例#1
0
 public SchedulerTaskEventArgs(SchedulerTask task)
 {
     Task = task;
 }
        static void MainLoop()
        {
            while (!Server.IsShuttingDown)
            {
                DateTime ticksNow = DateTime.UtcNow;

                SchedulerTask[] taskListCache = taskCache;

                for (int i = 0; i < taskListCache.Length && !Server.IsShuttingDown; i++)
                {
                    SchedulerTask task = taskListCache[i];
                    if (task.IsStopped || task.NextTime > ticksNow)
                    {
                        continue;
                    }
                    if (task.IsRecurring && task.AdjustForExecutionTime)
                    {
                        task.NextTime += task.Interval;
                    }

                    if (task.IsBackground)
                    {
                        lock ( BackgroundTaskListLock ) {
                            BackgroundTasks.Enqueue(task);
                        }
                    }
                    else
                    {
                        task.IsExecuting = true;

#if DEBUG_SCHEDULER
                        FireEvent(TaskExecuting, task);
#endif

#if DEBUG
                        task.Callback(task);
                        task.IsExecuting = false;
#else
                        try {
                            task.Callback(task);
                        } catch (Exception ex) {
                            Logger.LogAndReportCrash("Exception thrown by ScheduledTask callback", "fCraft", ex, false);
                        } finally {
                            task.IsExecuting = false;
                        }
#endif

#if DEBUG_SCHEDULER
                        FireEvent(TaskExecuted, task);
#endif
                    }

                    if (!task.IsRecurring || task.MaxRepeats == 1)
                    {
                        task.Stop();
                        continue;
                    }
                    task.MaxRepeats--;

                    ticksNow = DateTime.UtcNow;
                    if (!task.AdjustForExecutionTime)
                    {
                        task.NextTime = ticksNow.Add(task.Interval);
                    }
                }

                Thread.Sleep(10);
            }
        }