Timer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // System.Timers.Timer
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. // The docs talk about server timers and such...
  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;
  31. using System.ComponentModel;
  32. using System.Threading;
  33. namespace System.Timers
  34. {
  35. [DefaultEventAttribute("Elapsed")]
  36. [DefaultProperty("Interval")]
  37. public class Timer : Component, ISupportInitialize
  38. {
  39. bool autoReset;
  40. bool enabled;
  41. double interval;
  42. ISynchronizeInvoke so;
  43. ManualResetEvent wait;
  44. [Category("Behavior")]
  45. [TimersDescription("Occurs when the Interval has elapsed.")]
  46. public event ElapsedEventHandler Elapsed;
  47. public Timer () : this (100)
  48. {
  49. }
  50. public Timer (double interval)
  51. {
  52. autoReset = true;
  53. enabled = false;
  54. Interval = interval;
  55. so = null;
  56. wait = null;
  57. }
  58. [Category("Behavior")]
  59. [DefaultValue(true)]
  60. [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
  61. public bool AutoReset
  62. {
  63. get { return autoReset; }
  64. set { autoReset = value; }
  65. }
  66. [Category("Behavior")]
  67. [DefaultValue(false)]
  68. [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
  69. public bool Enabled
  70. {
  71. get { return enabled; }
  72. set {
  73. if (enabled == value)
  74. return;
  75. enabled = value;
  76. if (value) {
  77. Thread t = new Thread (new ThreadStart (StartTimer));
  78. t.IsBackground = true;
  79. t.Start ();
  80. } else {
  81. StopTimer ();
  82. }
  83. }
  84. }
  85. [Category("Behavior")]
  86. [DefaultValue(100)]
  87. [RecommendedAsConfigurable(true)]
  88. [TimersDescription( "The number of milliseconds between timer events.")]
  89. public double Interval
  90. {
  91. get { return interval; }
  92. set {
  93. // The doc says 'less than 0', but 0 also throws the exception
  94. if (value <= 0)
  95. throw new ArgumentException ("Invalid value: " + interval, "interval");
  96. interval = value;
  97. }
  98. }
  99. public override ISite Site
  100. {
  101. get { return base.Site; }
  102. set { base.Site = value; }
  103. }
  104. [DefaultValue(null)]
  105. [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
  106. "when an interval has elapsed.")]
  107. public ISynchronizeInvoke SynchronizingObject
  108. {
  109. get { return so; }
  110. set { so = value; }
  111. }
  112. public void BeginInit ()
  113. {
  114. // Nothing to do
  115. }
  116. public void Close ()
  117. {
  118. StopTimer ();
  119. }
  120. public void EndInit ()
  121. {
  122. // Nothing to do
  123. }
  124. public void Start ()
  125. {
  126. Enabled = true;
  127. }
  128. public void Stop ()
  129. {
  130. Enabled = false;
  131. }
  132. protected override void Dispose (bool disposing)
  133. {
  134. Close ();
  135. base.Dispose (disposing);
  136. }
  137. static void Callback (object state)
  138. {
  139. Timer timer = (Timer) state;
  140. if (timer.Elapsed == null)
  141. return;
  142. ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
  143. if (timer.so != null && timer.so.InvokeRequired) {
  144. timer.so.BeginInvoke (timer.Elapsed, new object [2] {timer, arg});
  145. } else {
  146. timer.Elapsed (timer, arg);
  147. }
  148. }
  149. void StartTimer ()
  150. {
  151. wait = new ManualResetEvent (false);
  152. WaitCallback wc = new WaitCallback (Callback);
  153. while (enabled && wait.WaitOne ((int) interval, false) == false) {
  154. if (autoReset == false)
  155. enabled = false;
  156. ThreadPool.QueueUserWorkItem (wc, this);
  157. }
  158. wc = null;
  159. ((IDisposable) wait).Dispose ();
  160. wait = null;
  161. }
  162. void StopTimer ()
  163. {
  164. if (wait != null)
  165. wait.Set ();
  166. }
  167. }
  168. }