2008-01-10 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.Timers / Timer.cs
1 //
2 // System.Timers.Timer
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // The docs talk about server timers and such...
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System.ComponentModel;
34 using System.Threading;
35
36 namespace System.Timers
37 {
38         [DefaultEventAttribute("Elapsed")]
39         [DefaultProperty("Interval")]
40         public class Timer : Component, ISupportInitialize
41         {
42                 bool autoReset;
43                 bool enabled;
44                 bool exiting;
45                 double interval;
46                 ISynchronizeInvoke so;
47                 ManualResetEvent wait;
48                 WeakReference weak_thread;
49                 readonly object locker = new object ();
50
51                 [Category("Behavior")]
52                 [TimersDescription("Occurs when the Interval has elapsed.")]
53                 public event ElapsedEventHandler Elapsed;
54
55                 public Timer () : this (100)
56                 {
57                 }
58
59                 public Timer (double interval)
60                 {
61 #if NET_2_0
62                         // MSBUG: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296761
63                         if (interval > 0x7FFFFFFF)
64                                 throw new ArgumentException ("Invalid value: " + interval, "interval");
65 #endif
66
67                         autoReset = true;
68                         Interval = interval;
69                 }
70
71                 [Category("Behavior")]
72                 [DefaultValue(true)]
73                 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
74                 public bool AutoReset
75                 {
76                         get { return autoReset; }
77                         set { autoReset = value; }
78                 }
79
80                 [Category("Behavior")]
81                 [DefaultValue(false)]
82                 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
83                 public bool Enabled
84                 {
85                         get {
86                                 return enabled && !exiting;
87                         }
88                         set {
89                                 if (Enabled == value)
90                                         return;
91
92                                 enabled = value;
93                                 if (value) {
94                                         if (exiting)
95                                                 StopTimer ();
96                                         exiting = false;
97                                         wait = new ManualResetEvent (false);
98                                         Thread thread = new Thread (new ThreadStart (StartTimer));
99                                         weak_thread = new WeakReference (thread);
100                                         
101                                         thread.IsBackground = true;
102                                         thread.Start ();
103                                 } else {
104                                         StopTimer ();
105                                 }
106                         }
107                 }
108
109                 [Category("Behavior")]
110                 [DefaultValue(100)]
111                 [RecommendedAsConfigurable(true)]
112                 [TimersDescription( "The number of milliseconds between timer events.")]
113                 public double Interval
114                 {
115                         get { return interval; }
116                         set { 
117                                 // The doc says 'less than 0', but 0 also throws the exception
118                                 if (value <= 0)
119                                         throw new ArgumentException ("Invalid value: " + interval, "interval");
120
121                                 interval = value;
122                         }
123                 }
124
125                 public override ISite Site
126                 {
127                         get { return base.Site; }
128                         set { base.Site = value; }
129                 }
130
131                 [DefaultValue(null)]
132                 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
133                                             "when an interval has elapsed.")]
134 #if NET_2_0
135                 [Browsable (false)]
136 #endif
137                 public ISynchronizeInvoke SynchronizingObject
138                 {
139                         get { return so; }
140                         set { so = value; }
141                 }
142
143                 public void BeginInit ()
144                 {
145                         // Nothing to do
146                 }
147
148                 public void Close ()
149                 {
150                         Enabled = false;
151                 }
152
153                 public void EndInit ()
154                 {
155                         // Nothing to do
156                 }
157
158                 public void Start ()
159                 {
160                         Enabled = true;
161                 }
162
163                 public void Stop ()
164                 {
165                         Enabled = false;
166                 }
167
168                 protected override void Dispose (bool disposing)
169                 {
170                         Close ();
171                         base.Dispose (disposing);
172                 }
173
174                 static void Callback (object state)
175                 {
176                         Timer timer = (Timer) state;
177                         if (timer.Elapsed == null)
178                                 return;
179
180                         ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
181
182                         if (timer.so != null && timer.so.InvokeRequired) {
183                                 timer.so.BeginInvoke (timer.Elapsed, new object [2] {timer, arg});
184                         } else {
185                                 timer.Elapsed (timer, arg);
186                         }
187                 }
188
189                 void StartTimer ()
190                 {
191                         WaitCallback wc = new WaitCallback (Callback);
192
193                         while (wait.WaitOne ((int) interval, false) == false) {
194                                 exiting = !autoReset;
195
196                                 ThreadPool.QueueUserWorkItem (wc, this);
197
198                                 if (exiting)
199                                         break;
200                         }
201
202                         lock (locker) {
203                                 wait.Close ();
204                                 wait = null;
205                         }
206                 }
207
208                 void StopTimer ()
209                 {
210                         lock (locker) {
211                                 if (wait != null)
212                                         wait.Set ();
213                         }
214
215                         // the sleep speeds up the join under linux
216                         Thread.Sleep (0);
217
218                         Thread thread = (Thread)weak_thread.Target;
219                         
220                         if (thread != null)
221                                 thread.Join ();
222                 }
223         }
224 }