Timer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. using System.Collections;
  32. namespace System.Threading
  33. {
  34. #if NET_2_0
  35. [ComVisible (true)]
  36. #endif
  37. public sealed class Timer : MarshalByRefObject, IDisposable
  38. {
  39. #region Timer instance fields
  40. TimerCallback callback;
  41. object state;
  42. long due_time_ms;
  43. long period_ms;
  44. long next_run; // in ticks
  45. bool disposed;
  46. #endregion
  47. #region Timer static fields
  48. static Thread scheduler;
  49. static Hashtable jobs;
  50. static AutoResetEvent change_event;
  51. #endregion
  52. /* we use a static initializer to avoid race issues with the thread creation */
  53. static Timer ()
  54. {
  55. change_event = new AutoResetEvent (false);
  56. jobs = new Hashtable ();
  57. scheduler = new Thread (SchedulerThread);
  58. scheduler.IsBackground = true;
  59. scheduler.Start ();
  60. }
  61. static long Ticks ()
  62. {
  63. /* use a monotonic time value later */
  64. return DateTime.UtcNow.Ticks;
  65. }
  66. static private void SchedulerThread ()
  67. {
  68. Thread.CurrentThread.Name = "Timer-Scheduler";
  69. while (true) {
  70. long min_wait = long.MaxValue;
  71. lock (jobs) {
  72. ArrayList expired = null;
  73. long ticks = Ticks ();
  74. foreach (DictionaryEntry entry in jobs) {
  75. Timer t1 = entry.Value as Timer;
  76. if (t1.next_run <= ticks) {
  77. ThreadPool.QueueUserWorkItem (new WaitCallback (t1.callback), t1.state);
  78. if (t1.period_ms == -1 || ((t1.period_ms == 0 | t1.period_ms == Timeout.Infinite) && t1.due_time_ms != Timeout.Infinite)) {
  79. t1.next_run = long.MaxValue;
  80. if (expired == null)
  81. expired = new ArrayList ();
  82. expired.Add (t1);
  83. } else {
  84. t1.next_run = ticks + TimeSpan.TicksPerMillisecond * t1.period_ms;
  85. }
  86. }
  87. if (t1.next_run != long.MaxValue) {
  88. min_wait = Math.Min (min_wait, t1.next_run - ticks);
  89. }
  90. }
  91. if (expired != null) {
  92. int count = expired.Count;
  93. for (int i = 0; i < count; ++i) {
  94. jobs.Remove (expired [i]);
  95. }
  96. expired.Clear ();
  97. if (count > 50)
  98. expired = null;
  99. }
  100. }
  101. if (min_wait != long.MaxValue) {
  102. change_event.WaitOne ((int)(min_wait / TimeSpan.TicksPerMillisecond), true);
  103. } else {
  104. change_event.WaitOne (Timeout.Infinite, true);
  105. }
  106. }
  107. }
  108. public Timer (TimerCallback callback, object state, int dueTime, int period)
  109. {
  110. if (dueTime < -1)
  111. throw new ArgumentOutOfRangeException ("dueTime");
  112. if (period < -1)
  113. throw new ArgumentOutOfRangeException ("period");
  114. Init (callback, state, dueTime, period);
  115. }
  116. public Timer (TimerCallback callback, object state, long dueTime, long period)
  117. {
  118. if (dueTime < -1)
  119. throw new ArgumentOutOfRangeException ("dueTime");
  120. if (period < -1)
  121. throw new ArgumentOutOfRangeException ("period");
  122. Init (callback, state, dueTime, period);
  123. }
  124. public Timer (TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
  125. : this (callback, state, (long)dueTime.TotalMilliseconds, (long)period.TotalMilliseconds)
  126. {
  127. }
  128. [CLSCompliant(false)]
  129. public Timer (TimerCallback callback, object state, uint dueTime, uint period)
  130. : this (callback, state, (long) dueTime, (long) period)
  131. {
  132. }
  133. #if NET_2_0
  134. public Timer (TimerCallback callback)
  135. {
  136. Init (callback, this, Timeout.Infinite, Timeout.Infinite);
  137. }
  138. #endif
  139. void Init (TimerCallback callback, object state, long dueTime, long period)
  140. {
  141. this.callback = callback;
  142. this.state = state;
  143. Change (dueTime, period);
  144. }
  145. public bool Change (int dueTime, int period)
  146. {
  147. return Change ((long)dueTime, (long)period);
  148. }
  149. public bool Change (long dueTime, long period)
  150. {
  151. if(dueTime > 4294967294)
  152. throw new NotSupportedException ("Due time too large");
  153. if(period > 4294967294)
  154. throw new NotSupportedException ("Period too large");
  155. if (dueTime < -1)
  156. throw new ArgumentOutOfRangeException ("dueTime");
  157. if (period < -1)
  158. throw new ArgumentOutOfRangeException ("period");
  159. if (disposed)
  160. return false;
  161. due_time_ms = dueTime;
  162. period_ms = period;
  163. if (dueTime == 0) {
  164. next_run = Ticks ();
  165. } else if (dueTime == Timeout.Infinite) {
  166. next_run = long.MaxValue;
  167. } else {
  168. next_run = dueTime * TimeSpan.TicksPerMillisecond + Ticks ();
  169. }
  170. lock (jobs) {
  171. if (next_run != long.MaxValue) {
  172. Timer t = jobs [this] as Timer;
  173. if (t == null)
  174. jobs [this] = this;
  175. change_event.Set ();
  176. } else {
  177. jobs.Remove (this);
  178. }
  179. }
  180. return true;
  181. }
  182. public bool Change (TimeSpan dueTime, TimeSpan period)
  183. {
  184. return Change ((long)dueTime.TotalMilliseconds, (long)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 ((long) dueTime, (long) period);
  194. }
  195. public void Dispose ()
  196. {
  197. disposed = true;
  198. lock (jobs) {
  199. jobs.Remove (this);
  200. }
  201. }
  202. public bool Dispose (WaitHandle notifyObject)
  203. {
  204. Dispose ();
  205. NativeEventCalls.SetEvent_internal (notifyObject.Handle);
  206. return true;
  207. }
  208. }
  209. }