Пример #1
0
 private void ScheduleTask(TimerTaskDescriptor task)
 {
     lock (tasksHeap)
     {
         tasksHeap.AddElement(task);
         System.Threading.Monitor.Pulse(tasksHeap);
     }
 }
Пример #2
0
        private void Run()
        {
            TimerTaskDescriptor taskToProcess = null;

            while (isRunning)
            {
                TimeSpan delay;
                lock (tasksHeap)
                {
                    TimerTaskDescriptor task;
                    if (tasksHeap.PollTopElement(out task))
                    {
                        var taskTime    = task.StartTime;
                        var currentTime = DateTime.UtcNow;
                        if (taskTime <= currentTime)
                        {
                            taskToProcess = task;
                            delay         = TimeSpan.Zero;
                        }
                        else
                        {
                            tasksHeap.AddElement(task);
                            delay = taskTime - currentTime;
                        }
                    }
                    else
                    {
                        delay = defaultIdleSleepTime;
                    }
                }
                if (taskToProcess != null)
                {
                    try
                    {
                        taskToProcess.DoAction();
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorFormat(GetType(), "The processing of the scheduled task has failed: {0}", ex);
                    }
                    if (taskToProcess.RepeatInterval > TimeSpan.Zero && !taskToProcess.IsCancelled())
                    {
                        ReScheduleTask(taskToProcess);
                    }
                    taskToProcess = null;
                }
                if (delay <= TimeSpan.Zero)
                {
                    continue;
                }
                lock (tasksHeap)
                {
                    if (isRunning)
                    {
                        try
                        {
                            System.Threading.Monitor.Wait(tasksHeap, delay);
                        }
                        catch (ThreadInterruptedException)
                        {
                            // Thread interrupt is just ignored
                        }
                    }
                }
            }
        }
Пример #3
0
 private static int TimerTaskComparer([NotNull] TimerTaskDescriptor task1, [NotNull] TimerTaskDescriptor task2)
 {
     return(task2.StartTime.CompareTo(task1.StartTime));
 }
Пример #4
0
        public void ScheduleTask(Action toExecute, TimeSpan delay, TimeSpan repeatInterval)
        {
            var taskDescriptor = new TimerTaskDescriptor(new TimerActionTask(toExecute), DateTime.UtcNow + delay, repeatInterval);

            ScheduleTask(taskDescriptor);
        }
Пример #5
0
        private void ReScheduleTask(TimerTaskDescriptor taskToProcess)
        {
            var when = taskToProcess.StartTime + taskToProcess.RepeatInterval;

            ScheduleTask(new TimerTaskDescriptor(taskToProcess.Task, when, taskToProcess.RepeatInterval));
        }