// // Method that runs the timer thread. // private static void Run() { for (;;) { // // Free up any Timer's awaiting destruction. Once this // is done the Timer is guarenteed to never fire again. // for (;;) { Timer timer; lock (typeof(Timer)) { if (Timer.disposeQueue.Count == 0) { break; } timer = (Timer)Timer.disposeQueue.Dequeue(); } WaitHandle notifyObject; lock (timer) notifyObject = timer.notifyObject; (notifyObject as ISignal).Signal(); } // // Sleep until an alarm is due to go off. // long longMs = Timer.alarmClock.TimeTillAlarm; int ms = longMs > int.MaxValue ? int.MaxValue : (int)longMs; Timer.threadWakeup.WaitOne(ms, false); Timer.AdvanceTime(ms); } }
// // Change the current timer parameters. // public bool Change(int dueTime, int period) { // // Validate the parameters. // if (dueTime < -1) { throw new ArgumentOutOfRangeException ("dueTime", _("ArgRange_NonNegOrNegOne")); } if (period < -1) { throw new ArgumentOutOfRangeException ("period", _("ArgRange_NonNegOrNegOne")); } // // Apparently trying to change the state of a disposed timer // is legal. // lock (this) { if (this.disposed) { return(false); } } // // Set the new state. // long due = dueTime == -1 ? AlarmClock.INFINITE : dueTime; long interval = period <= 0 ? AlarmClock.INFINITE : period; Timer.AdvanceTime(period); this.alarm.SetAlarm(due, interval); // // Wake up the background thread so it sees the new state. // Timer.threadWakeup.Set(); return(true); }