Ejemplo n.º 1
0
            private void DownHeap(int pos)
            {
                int current = pos;
                int child   = 2 * current + 1;

                while (child < size && size > 0)
                {
                    // compare the children if they exist
                    if (child + 1 < size && timers[child + 1].when < timers[child].when)
                    {
                        child++;
                    }

                    // compare selected child with parent
                    if (timers[current].when < timers[child].when)
                    {
                        break;
                    }

                    // swap the two
                    TimerTask tmp = timers[current];
                    timers[current] = timers[child];
                    timers[child]   = tmp;

                    // update pos and current
                    current = child;
                    child   = 2 * current + 1;
                }
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Schedules the specified TimerTask for repeated fixed-rate execution, beginning
        /// after the specified delay. Subsequent executions take place at approximately regular
        /// intervals, separated by the specified period.
        ///
        /// In fixed-rate execution, each execution is scheduled relative to the scheduled execution
        /// time of the initial execution. If an execution is delayed for any reason (such as garbage
        /// collection or other background activity), two or more executions will occur in rapid
        /// succession to "catch up."
        ///
        /// Fixed-rate execution is appropriate for recurring activities that are sensitive to
        /// absolute time, such as ringing a chime every hour on the hour, or running scheduled
        /// maintenance every day at a particular time.
        /// </summary>
        public void ScheduleAtFixedRate(TimerTask task, TimeSpan delay, TimeSpan period)
        {
            if (delay.CompareTo(TimeSpan.Zero) < 0 || period.CompareTo(TimeSpan.Zero) <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            DoScheduleImpl(task, delay, period, true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Schedules the specified TimerTask for execution after the specified delay.
        /// </summary>
        public void Schedule(TimerTask task, TimeSpan delay)
        {
            if (delay.CompareTo(TimeSpan.Zero) < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            DoScheduleImpl(task, delay, TimeSpan.FromMilliseconds(-1), false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Schedules the specified TimerTask for repeated fixed-rate execution, beginning
        /// after the specified delay. Subsequent executions take place at approximately regular
        /// intervals, separated by the specified period.
        ///
        /// In fixed-rate execution, each execution is scheduled relative to the scheduled execution
        /// time of the initial execution. If an execution is delayed for any reason (such as garbage
        /// collection or other background activity), two or more executions will occur in rapid
        /// succession to "catch up."
        ///
        /// Fixed-rate execution is appropriate for recurring activities that are sensitive to
        /// absolute time, such as ringing a chime every hour on the hour, or running scheduled
        /// maintenance every day at a particular time.
        /// </summary>
        public void ScheduleAtFixedRate(TimerTask task, int delay, int period)
        {
            if (delay < 0 || period <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            DoScheduleImpl(task, TimeSpan.FromMilliseconds(delay), TimeSpan.FromMilliseconds(period), true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Schedules the specified TimerTask for execution after the specified delay.
        /// </summary>
        public void Schedule(TimerTask task, int delay)
        {
            if (delay < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            DoScheduleImpl(task, TimeSpan.FromMilliseconds(delay), TimeSpan.FromMilliseconds(-1), false);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Executes the given task periodically using a fixed-delay execution style
 /// which prevents tasks from bunching should there be some delay such as
 /// garbage collection or machine sleep.
 ///
 /// This repeating unit of work can later be cancelled using the WaitCallback
 /// that was originally used to initiate the processing.
 /// </summary>
 public void ExecutePeriodically(WaitCallback task, object arg, TimeSpan period)
 {
     lock (this.syncRoot)
     {
         CheckStarted();
         TimerTask timerTask = timer.Schedule(task, arg, period, period);
         timerTasks.Add(task, timerTask);
     }
 }
Ejemplo n.º 7
0
 internal int GetTask(TimerTask task)
 {
     for (int i = 0; i < timers.Length; i++)
     {
         if (timers[i] == task)
         {
             return(i);
         }
     }
     return(-1);
 }
Ejemplo n.º 8
0
 public void Insert(TimerTask task)
 {
     if (timers.Length == size)
     {
         TimerTask[] appendedTimers = new TimerTask[size * 2];
         timers.CopyTo(appendedTimers, 0);
         timers = appendedTimers;
     }
     timers[size++] = task;
     UpHeap();
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Schedules the specified TimerTask for repeated fixed-rate execution, beginning
        /// at the specified time. Subsequent executions take place at approximately regular
        /// intervals, separated by the specified period.
        ///
        /// In fixed-rate execution, each execution is scheduled relative to the scheduled execution
        /// time of the initial execution. If an execution is delayed for any reason (such as garbage
        /// collection or other background activity), two or more executions will occur in rapid
        /// succession to "catch up."
        ///
        /// Fixed-rate execution is appropriate for recurring activities that are sensitive to
        /// absolute time, such as ringing a chime every hour on the hour, or running scheduled
        /// maintenance every day at a particular time.
        /// </summary>
        public void ScheduleAtFixedRate(TimerTask task, DateTime when, TimeSpan period)
        {
            if (period.CompareTo(TimeSpan.Zero) <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            TimeSpan delay = when - DateTime.Now;

            DoScheduleImpl(task, delay, period, true);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Schedules the specified TimerTask for repeated fixed-rate execution, beginning
        /// at the specified time. Subsequent executions take place at approximately regular
        /// intervals, separated by the specified period.
        ///
        /// In fixed-rate execution, each execution is scheduled relative to the scheduled execution
        /// time of the initial execution. If an execution is delayed for any reason (such as garbage
        /// collection or other background activity), two or more executions will occur in rapid
        /// succession to "catch up."
        ///
        /// Fixed-rate execution is appropriate for recurring activities that are sensitive to
        /// absolute time, such as ringing a chime every hour on the hour, or running scheduled
        /// maintenance every day at a particular time.
        /// </summary>
        public void ScheduleAtFixedRate(TimerTask task, DateTime when, int period)
        {
            if (period <= 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            TimeSpan delay = when - DateTime.Now;

            DoScheduleImpl(task, delay, TimeSpan.FromMilliseconds(period), true);
        }
Ejemplo n.º 11
0
            private void UpHeap()
            {
                int current = size - 1;
                int parent  = (current - 1) / 2;

                while (timers[current].when < timers[parent].when)
                {
                    // swap the two
                    TimerTask tmp = timers[current];
                    timers[current] = timers[parent];
                    timers[parent]  = tmp;

                    // update pos and current
                    current = parent;
                    parent  = (current - 1) / 2;
                }
            }
Ejemplo n.º 12
0
        public void Cancel(object task)
        {
            lock (this.syncRoot)
            {
                if (timerTasks.ContainsKey(task))
                {
                    TimerTask ticket = timerTasks[task];
                    if (ticket != null)
                    {
                        ticket.Cancel();
                        timer.Purge();         // remove cancelled TimerTasks
                    }

                    timerTasks.Remove(task);
                }
            }
        }
Ejemplo n.º 13
0
        private void DoScheduleImpl(TimerTask task, TimeSpan delay, TimeSpan period, bool fixedRate)
        {
            if (task == null)
            {
                throw new ArgumentNullException("TimerTask cannot be null");
            }

            lock (this.impl.SyncRoot)
            {
                if (impl.Cancelled)
                {
                    throw new InvalidOperationException();
                }

                DateTime when = DateTime.Now + delay;

                lock (task.syncRoot)
                {
                    if (task.IsScheduled)
                    {
                        throw new InvalidOperationException();
                    }

                    if (task.cancelled)
                    {
                        throw new InvalidOperationException("Task is already cancelled");
                    }

                    task.when      = when;
                    task.period    = period;
                    task.fixedRate = fixedRate;
                }

                // insert the newTask into queue
                impl.InsertTask(task);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Schedules the specified TimerTask for execution at the specified time. If the
        /// time is in the past.
        /// </summary>
        public void Schedule(TimerTask task, DateTime when)
        {
            TimeSpan delay = when - DateTime.Now;

            DoScheduleImpl(task, delay, TimeSpan.FromMilliseconds(-1), false);
        }
Ejemplo n.º 15
0
 public void InsertTask(TimerTask newTask)
 {
     // callers are synchronized
     tasks.Insert(newTask);
     Monitor.Pulse(this.syncRoot);
 }