5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // The docs talk about server timers and such...
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.ComponentModel
;
34 using System
.Threading
;
36 namespace System
.Timers
38 [DefaultEventAttribute("Elapsed")]
39 [DefaultProperty("Interval")]
40 public class Timer
: Component
, ISupportInitialize
45 ISynchronizeInvoke so
;
46 ManualResetEvent wait
;
48 [Category("Behavior")]
49 [TimersDescription("Occurs when the Interval has elapsed.")]
50 public event ElapsedEventHandler Elapsed
;
52 public Timer () : this (100)
56 public Timer (double interval
)
66 [Category("Behavior")]
68 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
71 get { return autoReset; }
72 set { autoReset = value; }
75 [Category("Behavior")]
77 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
80 get { return enabled; }
87 Thread t
= new Thread (new ThreadStart (StartTimer
));
88 t
.IsBackground
= true;
96 [Category("Behavior")]
98 [RecommendedAsConfigurable(true)]
99 [TimersDescription( "The number of milliseconds between timer events.")]
100 public double Interval
102 get { return interval; }
104 // The doc says 'less than 0', but 0 also throws the exception
106 throw new ArgumentException ("Invalid value: " + interval
, "interval");
112 public override ISite Site
114 get { return base.Site; }
115 set { base.Site = value; }
119 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
120 "when an interval has elapsed.")]
121 public ISynchronizeInvoke SynchronizingObject
127 public void BeginInit ()
137 public void EndInit ()
152 protected override void Dispose (bool disposing
)
155 base.Dispose (disposing
);
158 static void Callback (object state
)
160 Timer timer
= (Timer
) state
;
161 if (timer
.Elapsed
== null)
164 ElapsedEventArgs arg
= new ElapsedEventArgs (DateTime
.Now
);
166 if (timer
.so
!= null && timer
.so
.InvokeRequired
) {
167 timer
.so
.BeginInvoke (timer
.Elapsed
, new object [2] {timer, arg}
);
169 timer
.Elapsed (timer
, arg
);
175 wait
= new ManualResetEvent (false);
177 WaitCallback wc
= new WaitCallback (Callback
);
178 while (enabled
&& wait
.WaitOne ((int) interval
, false) == false) {
179 if (autoReset
== false)
182 ThreadPool
.QueueUserWorkItem (wc
, this);
186 ((IDisposable
) wait
).Dispose ();