2 // System.Threading.Timer.cs
5 // Dick Porter (dick@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
9 // (C) 2004 Novell, Inc. http://www.novell.com
13 namespace System.Threading
15 public sealed class Timer : MarshalByRefObject, IDisposable
17 sealed class Runner : MarshalByRefObject
19 ManualResetEvent wait;
20 AutoResetEvent start_event;
21 TimerCallback callback;
28 public Runner (TimerCallback callback, object state, AutoResetEvent start_event)
30 this.callback = callback;
32 this.start_event = start_event;
33 this.wait = new ManualResetEvent (false);
37 get { return dueTime; }
38 set { dueTime = value; }
42 get { return period; }
43 set { period = value == 0 ? Timeout.Infinite : value; }
46 bool WaitForDueTime ()
52 signaled = wait.WaitOne (dueTime, false);
53 } while (signaled == true && !disposed && !aborted);
77 while (start_event.WaitOne ()) {
80 if (dueTime == Timeout.Infinite)
83 if (!WaitForDueTime ())
86 if (aborted || (period == Timeout.Infinite))
89 bool signaled = false;
98 signaled = wait.WaitOne (period, false);
102 } else if (!WaitForDueTime ()) {
111 AutoResetEvent start_event;
114 public Timer (TimerCallback callback, object state, int dueTime, int period)
117 throw new ArgumentOutOfRangeException ("dueTime");
120 throw new ArgumentOutOfRangeException ("period");
122 Init (callback, state, dueTime, period);
125 public Timer (TimerCallback callback, object state, long dueTime, long period)
128 throw new ArgumentOutOfRangeException ("dueTime");
131 throw new ArgumentOutOfRangeException ("period");
133 Init (callback, state, (int) dueTime, (int) period);
136 public Timer (TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
137 : this (callback, state, Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds))
141 [CLSCompliant(false)]
142 public Timer (TimerCallback callback, object state, uint dueTime, uint period)
143 : this (callback, state, (long) dueTime, (long) period)
147 void Init (TimerCallback callback, object state, int dueTime, int period)
149 start_event = new AutoResetEvent (false);
150 runner = new Runner (callback, state, start_event);
151 Change (dueTime, period);
152 t = new Thread (new ThreadStart (runner.Start));
153 t.IsBackground = true;
157 [MonoTODO("false return?")]
158 public bool Change (int dueTime, int period)
161 throw new ArgumentOutOfRangeException ("dueTime");
164 throw new ArgumentOutOfRangeException ("period");
166 runner.DueTime = dueTime;
167 runner.Period = period;
173 public bool Change (long dueTime, long period)
175 if(dueTime > 4294967294)
176 throw new NotSupportedException ("Due time too large");
178 if(period > 4294967294)
179 throw new NotSupportedException ("Period too large");
181 return Change ((int) dueTime, (int) period);
184 public bool Change (TimeSpan dueTime, TimeSpan period)
186 return Change (Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds));
189 [CLSCompliant(false)]
190 public bool Change (uint dueTime, uint period)
192 if (dueTime > Int32.MaxValue)
193 throw new NotSupportedException ("Due time too large");
195 if (period > Int32.MaxValue)
196 throw new NotSupportedException ("Period too large");
198 return Change ((int) dueTime, (int) period);
201 public void Dispose ()
203 if (t != null && t.IsAlive) {
209 GC.SuppressFinalize (this);
212 [MonoTODO("How do we signal the handler?")]
213 public bool Dispose (WaitHandle notifyObject)
221 if (t != null && t.IsAlive)