5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // The docs talk about server timers and such...
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // 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 {
43 System.Threading.Timer timer;
44 object _lock = new object ();
45 ISynchronizeInvoke so;
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)
58 // MSBUG: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296761
59 if (interval <= 0 || interval > 0x7FFFFFFF)
60 throw new ArgumentException ("Invalid value: " + interval, "interval");
63 timer = new System.Threading.Timer (Callback, this, Timeout.Infinite, Timeout.Infinite); //disabled
67 [Category("Behavior")]
69 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
72 get { return autoReset; }
73 set { autoReset = value; }
76 [Category("Behavior")]
78 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
83 return enabled && timer != null;
88 throw new ObjectDisposedException (GetType ().ToString (), "The object has been disposed");
94 // As per MS docs (throw this only when the timer becomes enabled): http://msdn.microsoft.com/en-us/library/system.timers.timer.enabled(v=vs.110).aspx
95 if (interval > Int32.MaxValue)
96 throw new ArgumentException ("Invalid value: " + interval, "interval");
98 timer.Change ((int)interval, autoReset ? (int)interval : 0);
101 timer.Change (Timeout.Infinite, Timeout.Infinite);
107 [Category("Behavior")]
109 [RecommendedAsConfigurable(true)]
110 [TimersDescription( "The number of milliseconds between timer events.")]
111 public double Interval
113 get { return interval; }
115 // The doc says 'less than 0', but 0 also throws the exception
117 throw new ArgumentException ("Invalid value: " + value);
118 // As per MS docs (throw only if enabled, otherwise postpone throwing until it becomes enabled): http://msdn.microsoft.com/en-us/library/system.timers.timer.interval(v=vs.110).aspx
119 if (value > Int32.MaxValue && enabled)
120 throw new ArgumentException ("Invalid value: " + value);
126 //call Change only if enabled, otherwise it will be called when Enabled = true, see the comment above on throwing ArgumentException
128 timer.Change ((int)interval, autoReset? (int)interval: 0);
133 public override ISite Site
135 get { return base.Site; }
136 set { base.Site = value; }
140 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
141 "when an interval has elapsed.")]
143 public ISynchronizeInvoke SynchronizingObject
149 public void BeginInit ()
160 public void EndInit ()
175 protected override void Dispose (bool disposing)
177 // Could call Close() twice
181 // If we're disposing explicitly, clear all
182 // fields. If not, all fields will have been
183 // nulled by the GC during finalization, so
184 // trying to lock on _lock will blow up.
191 base.Dispose (disposing);
194 static void Callback (object state)
196 Timer timer = (Timer) state;
197 if (timer.Enabled == false)
199 ElapsedEventHandler events = timer.Elapsed;
203 if (!timer.autoReset)
204 timer.Enabled = false; //this could throw ObjectDisposed if timer.Close() was just called, after the check for Enabled above
206 catch (ObjectDisposedException) {
207 //Probably the Elapsed event should not fire if this Timer is found here to be closed
211 //If another thread calls Close() when this thread is right here (of further down), the Elapsed event might get called once more after this Timer was Closed()
212 //It's not a problem, it happens with all Timers, but it's good to know...
217 ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
219 if (timer.so != null && timer.so.InvokeRequired) {
220 timer.so.BeginInvoke (events, new object [2] {timer, arg});