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
46 ISynchronizeInvoke so;
47 ManualResetEvent wait;
48 WeakReference weak_thread;
49 readonly object locker = new object ();
51 [Category("Behavior")]
52 [TimersDescription("Occurs when the Interval has elapsed.")]
53 public event ElapsedEventHandler Elapsed;
55 public Timer () : this (100)
59 public Timer (double interval)
62 // MSBUG: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296761
63 if (interval > 0x7FFFFFFF)
64 throw new ArgumentException ("Invalid value: " + interval, "interval");
71 [Category("Behavior")]
73 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
76 get { return autoReset; }
77 set { autoReset = value; }
80 [Category("Behavior")]
82 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
86 return enabled && !exiting;
97 wait = new ManualResetEvent (false);
98 Thread thread = new Thread (new ThreadStart (StartTimer));
99 weak_thread = new WeakReference (thread);
101 thread.IsBackground = true;
109 [Category("Behavior")]
111 [RecommendedAsConfigurable(true)]
112 [TimersDescription( "The number of milliseconds between timer events.")]
113 public double Interval
115 get { return interval; }
117 // The doc says 'less than 0', but 0 also throws the exception
119 throw new ArgumentException ("Invalid value: " + interval, "interval");
125 public override ISite Site
127 get { return base.Site; }
128 set { base.Site = value; }
132 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
133 "when an interval has elapsed.")]
137 public ISynchronizeInvoke SynchronizingObject
143 public void BeginInit ()
153 public void EndInit ()
168 protected override void Dispose (bool disposing)
171 base.Dispose (disposing);
174 static void Callback (object state)
176 Timer timer = (Timer) state;
177 if (timer.Elapsed == null)
180 ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
182 if (timer.so != null && timer.so.InvokeRequired) {
183 timer.so.BeginInvoke (timer.Elapsed, new object [2] {timer, arg});
185 timer.Elapsed (timer, arg);
191 WaitCallback wc = new WaitCallback (Callback);
193 while (wait.WaitOne ((int) interval, false) == false) {
194 exiting = !autoReset;
196 ThreadPool.QueueUserWorkItem (wc, this);
215 // the sleep speeds up the join under linux
218 Thread thread = (Thread)weak_thread.Target;