Timer.cs 6.3 KB

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