Timer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. wait.Reset ();
  113. signaled = wait.WaitOne (period, false);
  114. if (aborted)
  115. break;
  116. if (!signaled) {
  117. callback (state);
  118. } else if (!WaitForDueTime ()) {
  119. return;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. Runner runner;
  126. AutoResetEvent start_event;
  127. Thread t;
  128. public Timer (TimerCallback callback, object state, int dueTime, int period)
  129. {
  130. if (dueTime < -1)
  131. throw new ArgumentOutOfRangeException ("dueTime");
  132. if (period < -1)
  133. throw new ArgumentOutOfRangeException ("period");
  134. Init (callback, state, dueTime, period);
  135. }
  136. public Timer (TimerCallback callback, object state, long dueTime, long period)
  137. {
  138. if (dueTime < -1)
  139. throw new ArgumentOutOfRangeException ("dueTime");
  140. if (period < -1)
  141. throw new ArgumentOutOfRangeException ("period");
  142. Init (callback, state, (int) dueTime, (int) period);
  143. }
  144. public Timer (TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
  145. : this (callback, state, Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds))
  146. {
  147. }
  148. [CLSCompliant(false)]
  149. public Timer (TimerCallback callback, object state, uint dueTime, uint period)
  150. : this (callback, state, (long) dueTime, (long) period)
  151. {
  152. }
  153. #if NET_2_0
  154. public Timer (TimerCallback callback)
  155. {
  156. Init (callback, this, Timeout.Infinite, Timeout.Infinite);
  157. }
  158. #endif
  159. void Init (TimerCallback callback, object state, int dueTime, int period)
  160. {
  161. start_event = new AutoResetEvent (false);
  162. runner = new Runner (callback, state, start_event);
  163. Change (dueTime, period);
  164. t = new Thread (new ThreadStart (runner.Start));
  165. t.IsBackground = true;
  166. t.Start ();
  167. }
  168. public bool Change (int dueTime, int period)
  169. {
  170. if (dueTime < -1)
  171. throw new ArgumentOutOfRangeException ("dueTime");
  172. if (period < -1)
  173. throw new ArgumentOutOfRangeException ("period");
  174. if (runner == null)
  175. return false;
  176. start_event.Reset ();
  177. runner.Abort ();
  178. runner.DueTime = dueTime;
  179. runner.Period = period;
  180. start_event.Set ();
  181. return true;
  182. }
  183. public bool Change (long dueTime, long period)
  184. {
  185. if(dueTime > 4294967294)
  186. throw new NotSupportedException ("Due time too large");
  187. if(period > 4294967294)
  188. throw new NotSupportedException ("Period too large");
  189. return Change ((int) dueTime, (int) period);
  190. }
  191. public bool Change (TimeSpan dueTime, TimeSpan period)
  192. {
  193. return Change (Convert.ToInt32(dueTime.TotalMilliseconds), Convert.ToInt32(period.TotalMilliseconds));
  194. }
  195. [CLSCompliant(false)]
  196. public bool Change (uint dueTime, uint period)
  197. {
  198. if (dueTime > Int32.MaxValue)
  199. throw new NotSupportedException ("Due time too large");
  200. if (period > Int32.MaxValue)
  201. throw new NotSupportedException ("Period too large");
  202. return Change ((int) dueTime, (int) period);
  203. }
  204. public void Dispose ()
  205. {
  206. if (t != null && t.IsAlive) {
  207. if (t != Thread.CurrentThread)
  208. t.Abort ();
  209. t = null;
  210. }
  211. runner.Dispose ();
  212. runner = null;
  213. GC.SuppressFinalize (this);
  214. }
  215. public bool Dispose (WaitHandle notifyObject)
  216. {
  217. Dispose ();
  218. NativeEventCalls.SetEvent_internal (notifyObject.Handle);
  219. return true;
  220. }
  221. ~Timer ()
  222. {
  223. if (t != null && t.IsAlive)
  224. t.Abort ();
  225. if (runner != null)
  226. runner.Abort ();
  227. }
  228. }
  229. }