Timer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. using System;
  11. using System.ComponentModel;
  12. using System.Threading;
  13. namespace System.Timers
  14. {
  15. [DefaultEventAttribute("Elapsed")]
  16. [DefaultProperty("Interval")]
  17. public class Timer : Component, ISupportInitialize
  18. {
  19. bool autoReset;
  20. bool enabled;
  21. double interval;
  22. ISynchronizeInvoke so;
  23. ManualResetEvent wait;
  24. [Category("Behavior")]
  25. [TimersDescription("Occurs when the Interval has elapsed.")]
  26. public event ElapsedEventHandler Elapsed;
  27. public Timer () : this (100)
  28. {
  29. }
  30. public Timer (double interval)
  31. {
  32. autoReset = true;
  33. enabled = false;
  34. Interval = interval;
  35. so = null;
  36. wait = null;
  37. }
  38. [Category("Behavior")]
  39. [DefaultValue(true)]
  40. [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
  41. public bool AutoReset
  42. {
  43. get { return autoReset; }
  44. set { autoReset = value; }
  45. }
  46. [Category("Behavior")]
  47. [DefaultValue(false)]
  48. [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
  49. public bool Enabled
  50. {
  51. get { return enabled; }
  52. set {
  53. if (enabled == value)
  54. return;
  55. enabled = value;
  56. if (value) {
  57. Thread t = new Thread (new ThreadStart (StartTimer));
  58. t.Start ();
  59. } else {
  60. StopTimer ();
  61. }
  62. }
  63. }
  64. [Category("Behavior")]
  65. [DefaultValue(100)]
  66. [RecommendedAsConfigurable(true)]
  67. [TimersDescription( "The number of milliseconds between timer events.")]
  68. public double Interval
  69. {
  70. get { return interval; }
  71. set {
  72. // The doc says 'less than 0', but 0 also throws the exception
  73. if (value <= 0)
  74. throw new ArgumentException ("Invalid value: " + interval, "interval");
  75. interval = value;
  76. }
  77. }
  78. public override ISite Site
  79. {
  80. get { return base.Site; }
  81. set { base.Site = value; }
  82. }
  83. [DefaultValue(null)]
  84. [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
  85. "when an interval has elapsed.")]
  86. public ISynchronizeInvoke SynchronizingObject
  87. {
  88. get { return so; }
  89. set { so = value; }
  90. }
  91. public void BeginInit ()
  92. {
  93. // Nothing to do
  94. }
  95. public void Close ()
  96. {
  97. StopTimer ();
  98. }
  99. public void EndInit ()
  100. {
  101. // Nothing to do
  102. }
  103. public void Start ()
  104. {
  105. Enabled = true;
  106. }
  107. public void Stop ()
  108. {
  109. Enabled = false;
  110. }
  111. protected override void Dispose (bool disposing)
  112. {
  113. Close ();
  114. base.Dispose (disposing);
  115. }
  116. static void Callback (object state)
  117. {
  118. Timer timer = (Timer) state;
  119. if (timer.Elapsed == null)
  120. return;
  121. ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
  122. if (timer.so != null && timer.so.InvokeRequired) {
  123. timer.so.BeginInvoke (timer.Elapsed, new object [2] {timer, arg});
  124. } else {
  125. timer.Elapsed (timer, arg);
  126. }
  127. }
  128. void StartTimer ()
  129. {
  130. wait = new ManualResetEvent (false);
  131. WaitCallback wc = new WaitCallback (Callback);
  132. while (enabled && wait.WaitOne ((int) interval, false) == false) {
  133. if (autoReset == false)
  134. enabled = false;
  135. ThreadPool.QueueUserWorkItem (wc, this);
  136. }
  137. wc = null;
  138. ((IDisposable) wait).Dispose ();
  139. wait = null;
  140. }
  141. void StopTimer ()
  142. {
  143. if (wait != null)
  144. wait.Set ();
  145. }
  146. }
  147. }