Timer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // System.Threading.Timer.cs
  3. //
  4. // Authors:
  5. // Dick Porter ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Runtime.InteropServices;
  31. namespace System.Threading
  32. {
  33. #if NET_2_0
  34. [ComVisible (true)]
  35. #endif
  36. public sealed class Timer : MarshalByRefObject, IDisposable
  37. {
  38. sealed class Runner : MarshalByRefObject
  39. {
  40. ManualResetEvent wait;
  41. AutoResetEvent start_event;
  42. TimerCallback callback;
  43. object state;
  44. int dueTime;
  45. int period;
  46. bool disposed;
  47. bool aborted;
  48. public Runner (TimerCallback callback, object state, AutoResetEvent start_event)
  49. {
  50. this.callback = callback;
  51. this.state = state;
  52. this.start_event = start_event;
  53. this.wait = new ManualResetEvent (false);
  54. }
  55. public int DueTime {
  56. get { return dueTime; }
  57. set { dueTime = value; }
  58. }
  59. public int Period {
  60. get { return period; }
  61. set { period = value == 0 ? Timeout.Infinite : value; }
  62. }
  63. bool WaitForDueTime ()
  64. {
  65. if (dueTime > 0) {
  66. bool signaled;
  67. do {
  68. wait.Reset ();
  69. signaled = wait.WaitOne (dueTime, false);
  70. } while (signaled == true && !disposed && !aborted);
  71. if (!signaled)
  72. callback (state);
  73. if (disposed)
  74. return false;
  75. }
  76. else
  77. callback (state);
  78. return true;
  79. }
  80. public void Abort ()
  81. {
  82. lock (this) {
  83. aborted = true;
  84. wait.Set ();
  85. }
  86. }
  87. public void Dispose ()
  88. {
  89. lock (this) {
  90. disposed = true;
  91. Abort ();
  92. }
  93. }
  94. public void Start ()
  95. {
  96. while (!disposed && start_event.WaitOne ()) {
  97. if (disposed)
  98. return;
  99. aborted = false;
  100. if (dueTime == Timeout.Infinite)
  101. continue;
  102. if (!WaitForDueTime ())
  103. return;
  104. if (aborted || (period == Timeout.Infinite))
  105. continue;
  106. bool signaled = false;
  107. while (true) {
  108. if (disposed)
  109. return;
  110. if (aborted)
  111. break;
  112. try {
  113. wait.Reset ();
  114. } catch (ObjectDisposedException) {
  115. // FIXME: There is some race condition
  116. // here when the thread is being
  117. // aborted on exit.
  118. return;
  119. }
  120. signaled = wait.WaitOne (period, false);
  121. if (aborted || disposed)
  122. break;
  123. if (!signaled) {
  124. callback (state);
  125. } else if (!WaitForDueTime ()) {
  126. return;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. Runner runner;
  133. AutoResetEvent start_event;
  134. WeakReference weak_t;
  135. public Timer (TimerCallback callback, object state, int dueTime, int period)
  136. {
  137. if (dueTime < -1)
  138. throw new ArgumentOutOfRangeException ("dueTime");
  139. if (period < -1)
  140. throw new ArgumentOutOfRangeException ("period");
  141. Init (callback, state, dueTime, period);
  142. }
  143. public Timer (TimerCallback callback, object state, long dueTime, long period)
  144. {
  145. if (dueTime < -1)
  146. throw new ArgumentOutOfRangeException ("dueTime");
  147. if (period < -1)
  148. throw new ArgumentOutOfRangeException ("period");
  149. Init (callback, state, (int) dueTime, (int) period);
  150. }
  151. public Timer (TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
  152. : this (callback, state, Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds))
  153. {
  154. }
  155. [CLSCompliant(false)]
  156. public Timer (TimerCallback callback, object state, uint dueTime, uint period)
  157. : this (callback, state, (long) dueTime, (long) period)
  158. {
  159. }
  160. #if NET_2_0
  161. public Timer (TimerCallback callback)
  162. {
  163. Init (callback, this, Timeout.Infinite, Timeout.Infinite);
  164. }
  165. #endif
  166. void Init (TimerCallback callback, object state, int dueTime, int period)
  167. {
  168. start_event = new AutoResetEvent (false);
  169. runner = new Runner (callback, state, start_event);
  170. Change (dueTime, period);
  171. Thread t = new Thread (new ThreadStart (runner.Start));
  172. weak_t = new WeakReference (t);
  173. t.IsBackground = true;
  174. t.Start ();
  175. }
  176. public bool Change (int dueTime, int period)
  177. {
  178. if (dueTime < -1)
  179. throw new ArgumentOutOfRangeException ("dueTime");
  180. if (period < -1)
  181. throw new ArgumentOutOfRangeException ("period");
  182. if (runner == null)
  183. return false;
  184. start_event.Reset ();
  185. runner.Abort ();
  186. runner.DueTime = dueTime;
  187. runner.Period = period;
  188. start_event.Set ();
  189. return true;
  190. }
  191. public bool Change (long dueTime, long period)
  192. {
  193. if(dueTime > 4294967294)
  194. throw new NotSupportedException ("Due time too large");
  195. if(period > 4294967294)
  196. throw new NotSupportedException ("Period too large");
  197. return Change ((int) dueTime, (int) period);
  198. }
  199. public bool Change (TimeSpan dueTime, TimeSpan period)
  200. {
  201. return Change (Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds));
  202. }
  203. [CLSCompliant(false)]
  204. public bool Change (uint dueTime, uint period)
  205. {
  206. if (dueTime > Int32.MaxValue)
  207. throw new NotSupportedException ("Due time too large");
  208. if (period > Int32.MaxValue)
  209. throw new NotSupportedException ("Period too large");
  210. return Change ((int) dueTime, (int) period);
  211. }
  212. public void Dispose ()
  213. {
  214. Thread t = (Thread)weak_t.Target;
  215. if (t != null && t.IsAlive) {
  216. if (t != Thread.CurrentThread)
  217. t.Abort ();
  218. }
  219. if (runner != null) {
  220. runner.Dispose ();
  221. runner = null;
  222. }
  223. GC.SuppressFinalize (this);
  224. }
  225. public bool Dispose (WaitHandle notifyObject)
  226. {
  227. Dispose ();
  228. NativeEventCalls.SetEvent_internal (notifyObject.Handle);
  229. return true;
  230. }
  231. ~Timer ()
  232. {
  233. Thread t = (Thread)weak_t.Target;
  234. if (t != null && t.IsAlive)
  235. t.Abort ();
  236. if (runner != null)
  237. runner.Abort ();
  238. }
  239. }
  240. }