Fix bug #311: On LinkedList.Clear, detach each node instead of dropping them en masse.
[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 #if MOONLIGHT
39         internal class Timer {
40 #else
41         [DefaultEventAttribute("Elapsed")]
42         [DefaultProperty("Interval")]
43         public class Timer : Component, ISupportInitialize {
44 #endif
45                 double interval;
46                 bool autoReset;
47                 System.Threading.Timer timer;
48                 object _lock = new object ();
49                 ISynchronizeInvoke so;
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                         // MSBUG: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=296761
62                         if (interval > 0x7FFFFFFF)
63                                 throw new ArgumentException ("Invalid value: " + interval, "interval");
64
65                         autoReset = true;
66                         Interval = interval;
67                 }
68
69                 [Category("Behavior")]
70                 [DefaultValue(true)]
71                 [TimersDescription("Indicates whether the timer will be restarted when it is enabled.")]
72                 public bool AutoReset
73                 {
74                         get { return autoReset; }
75                         set { autoReset = value; }
76                 }
77
78                 [Category("Behavior")]
79                 [DefaultValue(false)]
80                 [TimersDescription("Indicates whether the timer is enabled to fire events at a defined interval.")]
81                 public bool Enabled
82                 {
83                         get {
84                                 lock (_lock)
85                                         return timer != null;
86                         }
87                         set {
88                                 lock (_lock) {
89                                         bool enabled = timer != null;
90                                         if (enabled == value)
91                                                 return;
92
93                                         if (value) {
94                                                 timer = new System.Threading.Timer (Callback, this, (int)interval, autoReset ? (int)interval: 0);
95                                         } else {
96                                                 timer.Dispose ();
97                                                 timer = null;
98                                         }
99                                 }
100                         }
101                 }
102
103                 [Category("Behavior")]
104                 [DefaultValue(100)]
105                 [RecommendedAsConfigurable(true)]
106                 [TimersDescription( "The number of milliseconds between timer events.")]
107                 public double Interval
108                 {
109                         get { return interval; }
110                         set { 
111                                 // The doc says 'less than 0', but 0 also throws the exception
112                                 if (value <= 0)
113                                         throw new ArgumentException ("Invalid value: " + value);
114
115                                 lock (_lock) {
116                                         interval = value;
117                                         if (timer != null)
118                                                 timer.Change ((int)interval, autoReset? (int)interval: 0);
119                                 }
120                         }
121                 }
122
123 #if !MOONLIGHT
124                 public override ISite Site
125                 {
126                         get { return base.Site; }
127                         set { base.Site = value; }
128                 }
129 #endif
130                 [DefaultValue(null)]
131                 [TimersDescriptionAttribute("The object used to marshal the event handler calls issued " +
132                                             "when an interval has elapsed.")]
133                 [Browsable (false)]
134                 public ISynchronizeInvoke SynchronizingObject
135                 {
136                         get { return so; }
137                         set { so = value; }
138                 }
139
140                 public void BeginInit ()
141                 {
142                         // Nothing to do
143                 }
144
145                 public void Close ()
146                 {
147                         Enabled = false;
148                 }
149
150                 public void EndInit ()
151                 {
152                         // Nothing to do
153                 }
154
155                 public void Start ()
156                 {
157                         Enabled = true;
158                 }
159
160                 public void Stop ()
161                 {
162                         Enabled = false;
163                 }
164
165 #if MOONLIGHT
166                 protected void Dispose (bool disposing)
167                 {
168                         Close ();
169                 }
170 #else
171                 protected override void Dispose (bool disposing)
172                 {
173                         Close ();
174                         base.Dispose (disposing);
175                 }
176 #endif
177
178                 static void Callback (object state)
179                 {
180                         Timer timer = (Timer) state;
181                         if (timer.Enabled == false)
182                                 return;
183                         ElapsedEventHandler events = timer.Elapsed;
184                         if (!timer.autoReset)
185                                 timer.Enabled = false;
186                         if (events == null)
187                                 return;
188
189                         ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
190
191                         if (timer.so != null && timer.so.InvokeRequired) {
192                                 timer.so.BeginInvoke (events, new object [2] {timer, arg});
193                         } else {
194                                 try {
195                                         events (timer, arg);
196                                 } catch {
197                                 }
198                         }
199                 }
200
201         }
202 }