public int Compare(object x, object y)
        {
            QueuedEvent xx = x as QueuedEvent;
            QueuedEvent yy = y as QueuedEvent;

            return(xx.SchedTime.CompareTo(yy.SchedTime));
        }
Exemple #2
0
        /// <remarks>
        /// Get the first task, if the running time hasn't been
        /// reached then wait a bit and retry. Else reschedule the task and then
        /// run it.
        /// </remarks>
        /// <summary>
        /// If the task queue is empty, sleep until a task comes in or if slept
        /// for too long, suspend the thread.
        /// </summary>
        private void _run()
        {
            long elapsedTime;

            try
            {
                while (true)
                {
                    lock (this)
                    {
                        if (thread == null)
                        {
                            return;
                        }
                    }

                    Task task           = null;
                    bool lockReAcquired = true;
                    lock (queue)
                    {
                        if (queue.IsEmpty)
                        {
                            lockReAcquired = Monitor.Wait(queue, (int)suspend_interval);
                        }

                        if (lockReAcquired)
                        {
                            QueuedEvent e        = queue.Peek();
                            long        interval = e.Interval;
                            if (e != null)
                            {
                                lock (e)
                                {
                                    task = e.Task;
                                    if (task == null || task.IsCancelled())
                                    {
                                        queue.Pop();
                                        continue;
                                    }

                                    elapsedTime = e.ElapsedTime;
                                    if (elapsedTime >= interval)
                                    {
                                        // Reschedule the task
                                        queue.Pop();
                                        if (e.ReQueue())
                                        {
                                            queue.Push(e);
                                        }
                                    }
                                }
                                if (elapsedTime < interval)
                                {
                                    Monitor.Wait(queue, (int)(interval - elapsedTime));
                                    continue;
                                }
                            }
                        }
                    }

                    lock (this)
                    {
                        if (queue.IsEmpty && !lockReAcquired)
                        {
                            _suspend();
                            return;
                        }
                    }
                    try
                    {
                        //if(task != null)
                        //    task.Run();
                        if (task.IsRunning)
                        {
                            if (!task.RunAgain)
                            {
                                continue;
                            }
                        }
                        ThreadPool.QueueUserWorkItem(ExecuterCallback, task);
                    }
                    catch (Exception ex)
                    {
                        //Trace.error("TimeScheduler._run()", ex.ToString());
                    }
                }
            }
            catch (ThreadInterruptedException ex)
            {
                //Trace.error("TimeScheduler._run()",ex.ToString());
            }
        }