Merge branch 'patch-1' of https://github.com/ReubenBond/mono into ReubenBond-patch-1
[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                 double interval;
42                 bool autoReset;
43                 System.Threading.Timer timer;
44                 object _lock = new object ();
45                 ISynchronizeInvoke so;
46                 bool enabled;
47
48                 [Category("Behavior")]
49                 [TimersDescription("Occurs when the Interval has elapsed.")]
50                 public event ElapsedEventHandler Elapsed;
51
52                 public Timer () : this (100)
53                 {
54                 }
55
56                 public Timer (double interval)
57                 {
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");
61
62                         autoReset = true;
63                         timer = new System.Threading.Timer (Callback, this, Timeout.Infinite, Timeout.Infinite); //disabled
64                         Interval = interval;
65                 }
66
67                 [Category("Behavior")]
68                 [DefaultValue(true)]
69                 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
70                 public bool AutoReset
71                 {
72                         get { return autoReset; }
73                         set { autoReset = value; }
74                 }
75
76                 [Category("Behavior")]
77                 [DefaultValue(false)]
78                 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
79                 public bool Enabled
80                 {
81                         get {
82                                 lock (_lock)
83                                         return enabled && timer != null;
84                         }
85                         set {
86                                 lock (_lock) {
87                                         if (timer == null)
88                                                 throw new ObjectDisposedException (GetType ().ToString (), "The object has been disposed");
89                     
90                                         if (enabled == value)
91                                                 return;
92
93                                         if (value) {
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");
97                                                 enabled = true;
98                                                 timer.Change ((int)interval, autoReset ? (int)interval : 0);
99                                         } else {
100                                                 enabled = false;
101                                                 timer.Change (Timeout.Infinite, Timeout.Infinite);
102                                         }
103                                 }
104                         }
105                 }
106
107                 [Category("Behavior")]
108                 [DefaultValue(100)]
109                 [RecommendedAsConfigurable(true)]
110                 [TimersDescription( "The number of milliseconds between timer events.")]
111                 public double Interval
112                 {
113                         get { return interval; }
114                         set { 
115                                 // The doc says 'less than 0', but 0 also throws the exception
116                                 if (value <= 0)
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);
121
122                                 lock (_lock) {
123                                         if (timer == null)
124                                                 return;
125                                         interval = value;
126                                         //call Change only if enabled, otherwise it will be called when Enabled = true, see the comment above on throwing ArgumentException
127                                         if (enabled)
128                                                 timer.Change ((int)interval, autoReset? (int)interval: 0);
129                                 }
130                         }
131                 }
132
133                 public override ISite Site
134                 {
135                         get { return base.Site; }
136                         set { base.Site = value; }
137                 }
138
139                 [DefaultValue(null)]
140                 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
141                                             "when an interval has elapsed.")]
142                 [Browsable (false)]
143                 public ISynchronizeInvoke SynchronizingObject
144                 {
145                         get { return so; }
146                         set { so = value; }
147                 }
148
149                 public void BeginInit ()
150                 {
151                         // Nothing to do
152                 }
153
154                 public void Close ()
155                 {
156                         lock (_lock)
157                                 Dispose (true);
158                 }
159
160                 public void EndInit ()
161                 {
162                         // Nothing to do
163                 }
164
165                 public void Start ()
166                 {
167                         Enabled = true;
168                 }
169
170                 public void Stop ()
171                 {
172                         Enabled = false;
173                 }
174
175                 protected override void Dispose (bool disposing)
176                 {
177                         // Could call Close() twice
178                         if (timer == null)
179                                 return;
180
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.
185                         if (disposing)
186                         {
187                                 timer.Dispose ();
188                                 timer = null;
189                         }
190
191                         base.Dispose (disposing);
192                 }
193
194                 static void Callback (object state)
195                 {
196                         Timer timer = (Timer) state;
197                         if (timer.Enabled == false)
198                                 return;
199                         ElapsedEventHandler events = timer.Elapsed;
200
201                         try
202                         {
203                                 if (!timer.autoReset)
204                                         timer.Enabled = false; //this could throw ObjectDisposed if timer.Close() was just called, after the check for Enabled above
205                         }
206                         catch (ObjectDisposedException) {
207                                 //Probably the Elapsed event should not fire if this Timer is found here to be closed
208                                 return;
209                         }
210
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...
213
214                         if (events == null)
215                                 return;
216
217                         ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
218
219                         if (timer.so != null && timer.so.InvokeRequired) {
220                                 timer.so.BeginInvoke (events, new object [2] {timer, arg});
221                         } else {
222                                 try {
223                                         events (timer, arg);
224                                 } catch {
225                                 }
226                         }
227                 }
228
229         }
230 }