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