Timer.cs 6.1 KB

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