mscorlib(4.0.0.0) API with additions
Timer.cs
4 using System.Security;
6 using System.Threading;
7 
8 namespace System.Timers
9 {
11  [DefaultProperty("Interval")]
12  [DefaultEvent("Elapsed")]
13  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
15  {
16  internal struct FILE_TIME
17  {
18  internal int ftTimeLow;
19 
20  internal int ftTimeHigh;
21  }
22 
23  private double interval;
24 
25  private bool enabled;
26 
27  private bool initializing;
28 
29  private bool delayedEnable;
30 
31  private ElapsedEventHandler onIntervalElapsed;
32 
33  private bool autoReset;
34 
35  private ISynchronizeInvoke synchronizingObject;
36 
37  private bool disposed;
38 
39  private System.Threading.Timer timer;
40 
41  private TimerCallback callback;
42 
43  private object cookie;
44 
48  [Category("Behavior")]
49  [TimersDescription("TimerAutoReset")]
50  [DefaultValue(true)]
51  public bool AutoReset
52  {
53  get
54  {
55  return autoReset;
56  }
57  set
58  {
59  if (base.DesignMode)
60  {
61  autoReset = value;
62  }
63  else if (autoReset != value)
64  {
65  autoReset = value;
66  if (timer != null)
67  {
68  UpdateTimer();
69  }
70  }
71  }
72  }
73 
79  [Category("Behavior")]
80  [TimersDescription("TimerEnabled")]
81  [DefaultValue(false)]
82  public bool Enabled
83  {
84  get
85  {
86  return enabled;
87  }
88  set
89  {
90  if (base.DesignMode)
91  {
92  delayedEnable = value;
93  enabled = value;
94  }
95  else if (initializing)
96  {
97  delayedEnable = value;
98  }
99  else
100  {
101  if (enabled == value)
102  {
103  return;
104  }
105  if (!value)
106  {
107  if (timer != null)
108  {
109  cookie = null;
110  timer.Dispose();
111  timer = null;
112  }
113  enabled = value;
114  return;
115  }
116  enabled = value;
117  if (timer == null)
118  {
119  if (disposed)
120  {
121  throw new ObjectDisposedException(GetType().Name);
122  }
123  int num = (int)Math.Ceiling(interval);
124  cookie = new object();
125  timer = new System.Threading.Timer(callback, cookie, num, autoReset ? num : (-1));
126  }
127  else
128  {
129  UpdateTimer();
130  }
131  }
132  }
133  }
134 
138  [Category("Behavior")]
139  [TimersDescription("TimerInterval")]
140  [DefaultValue(100.0)]
141  [SettingsBindable(true)]
142  public double Interval
143  {
144  get
145  {
146  return interval;
147  }
148  set
149  {
150  if (value <= 0.0)
151  {
152  throw new ArgumentException(SR.GetString("TimerInvalidInterval", value, 0));
153  }
154  interval = value;
155  if (timer != null)
156  {
157  UpdateTimer();
158  }
159  }
160  }
161 
164  public override ISite Site
165  {
166  get
167  {
168  return base.Site;
169  }
170  set
171  {
172  base.Site = value;
173  if (base.DesignMode)
174  {
175  enabled = true;
176  }
177  }
178  }
179 
182  [Browsable(false)]
183  [DefaultValue(null)]
184  [TimersDescription("TimerSynchronizingObject")]
186  {
187  get
188  {
189  if (synchronizingObject == null && base.DesignMode)
190  {
191  IDesignerHost designerHost = (IDesignerHost)GetService(typeof(IDesignerHost));
192  if (designerHost != null)
193  {
194  object rootComponent = designerHost.RootComponent;
195  if (rootComponent != null && rootComponent is ISynchronizeInvoke)
196  {
197  synchronizingObject = (ISynchronizeInvoke)rootComponent;
198  }
199  }
200  }
201  return synchronizingObject;
202  }
203  set
204  {
205  synchronizingObject = value;
206  }
207  }
208 
210  [Category("Behavior")]
211  [TimersDescription("TimerIntervalElapsed")]
212  public event ElapsedEventHandler Elapsed
213  {
214  add
215  {
216  onIntervalElapsed = (ElapsedEventHandler)Delegate.Combine(onIntervalElapsed, value);
217  }
218  remove
219  {
220  onIntervalElapsed = (ElapsedEventHandler)Delegate.Remove(onIntervalElapsed, value);
221  }
222  }
223 
225  public Timer()
226  {
227  interval = 100.0;
228  enabled = false;
229  autoReset = true;
230  initializing = false;
231  delayedEnable = false;
232  callback = MyTimerCallback;
233  }
234 
238  public Timer(double interval)
239  : this()
240  {
241  if (interval <= 0.0)
242  {
243  throw new ArgumentException(SR.GetString("InvalidParameter", "interval", interval));
244  }
245  double num = Math.Ceiling(interval);
246  if (num > 2147483647.0 || num <= 0.0)
247  {
248  throw new ArgumentException(SR.GetString("InvalidParameter", "interval", interval));
249  }
250  this.interval = (int)num;
251  }
252 
253  private void UpdateTimer()
254  {
255  int num = (int)Math.Ceiling(interval);
256  timer.Change(num, autoReset ? num : (-1));
257  }
258 
260  public void BeginInit()
261  {
262  Close();
263  initializing = true;
264  }
265 
267  public void Close()
268  {
269  initializing = false;
270  delayedEnable = false;
271  enabled = false;
272  if (timer != null)
273  {
274  timer.Dispose();
275  timer = null;
276  }
277  }
278 
282  protected override void Dispose(bool disposing)
283  {
284  Close();
285  disposed = true;
286  base.Dispose(disposing);
287  }
288 
290  public void EndInit()
291  {
292  initializing = false;
293  Enabled = delayedEnable;
294  }
295 
298  public void Start()
299  {
300  Enabled = true;
301  }
302 
304  public void Stop()
305  {
306  Enabled = false;
307  }
308 
309  private void MyTimerCallback(object state)
310  {
311  if (state == cookie)
312  {
313  if (!autoReset)
314  {
315  enabled = false;
316  }
317  FILE_TIME lpSystemTimeAsFileTime = default(FILE_TIME);
318  GetSystemTimeAsFileTime(ref lpSystemTimeAsFileTime);
319  ElapsedEventArgs elapsedEventArgs = new ElapsedEventArgs(lpSystemTimeAsFileTime.ftTimeLow, lpSystemTimeAsFileTime.ftTimeHigh);
320  try
321  {
322  ElapsedEventHandler elapsedEventHandler = onIntervalElapsed;
323  if (elapsedEventHandler != null)
324  {
326  {
327  SynchronizingObject.BeginInvoke(elapsedEventHandler, new object[2]
328  {
329  this,
330  elapsedEventArgs
331  });
332  }
333  else
334  {
335  elapsedEventHandler(this, elapsedEventArgs);
336  }
337  }
338  }
339  catch
340  {
341  }
342  }
343  }
344 
345  [DllImport("kernel32.dll")]
346  [SuppressUnmanagedCodeSecurity]
347  internal static extern void GetSystemTimeAsFileTime(ref FILE_TIME lpSystemTimeAsFileTime);
348  }
349 }
void Stop()
Stops raising the E:System.Timers.Timer.Elapsed event by setting P:System.Timers.Timer....
Definition: Timer.cs:304
override ISite Site
Gets or sets the site that binds the T:System.Timers.Timer to its container in design mode.
Definition: Timer.cs:165
IComponent RootComponent
Gets the instance of the base class used as the root component for the current design.
Definition: __Canon.cs:3
Provides a way to synchronously or asynchronously execute a delegate.
void Close()
Releases the resources used by the T:System.Timers.Timer.
Definition: Timer.cs:267
ElapsedEventHandler Elapsed
Occurs when the interval elapses.
Definition: Timer.cs:213
static Delegate Remove(Delegate source, Delegate value)
Removes the last occurrence of the invocation list of a delegate from the invocation list of another ...
Definition: Delegate.cs:287
static Delegate Combine(Delegate a, Delegate b)
Concatenates the invocation lists of two delegates.
Definition: Delegate.cs:202
double Interval
Gets or sets the interval, expressed in milliseconds, at which to raise the E:System....
Definition: Timer.cs:143
bool InvokeRequired
Gets a value indicating whether the caller must call M:System.ComponentModel.ISynchronizeInvoke....
Timer()
Initializes a new instance of the T:System.Timers.Timer class, and sets all the properties to their i...
Definition: Timer.cs:225
The exception that is thrown when an operation is performed on a disposed object.
SecurityAction
Specifies the security actions that can be performed using declarative security.
void BeginInit()
Begins the run-time initialization of a T:System.Timers.Timer that is used on a form or by another co...
Definition: Timer.cs:260
delegate void TimerCallback(object state)
Represents the method that handles calls from a T:System.Threading.Timer.
Provides a mechanism for executing a method on a thread pool thread at specified intervals....
Definition: Timer.cs:12
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: Delegate.cs:15
Generates an event after a set interval, with an option to generate recurring events....
Definition: Timer.cs:14
override void Dispose(bool disposing)
Releases all resources used by the current T:System.Timers.Timer.
Definition: Timer.cs:282
virtual object GetService(Type service)
Returns an object that represents a service provided by the T:System.ComponentModel....
Definition: Component.cs:131
void Start()
Starts raising the E:System.Timers.Timer.Elapsed event by setting P:System.Timers....
Definition: Timer.cs:298
The exception that is thrown when one of the arguments provided to a method is not valid.
delegate void ElapsedEventHandler(object sender, ElapsedEventArgs e)
Represents the method that will handle the E:System.Timers.Timer.Elapsed event of a T:System....
void EndInit()
Ends the run-time initialization of a T:System.Timers.Timer that is used on a form or by another comp...
Definition: Timer.cs:290
Provides an interface for managing designer transactions and components.
Definition: IDesignerHost.cs:7
bool? Enabled
Gets or sets a value indicating whether the T:System.Timers.Timer should raise the E:System....
Definition: Timer.cs:83
bool AutoReset
Gets or sets a Boolean indicating whether the T:System.Timers.Timer should raise the E:System....
Definition: Timer.cs:52
IAsyncResult BeginInvoke(Delegate method, object[] args)
Asynchronously executes the delegate on the thread that created this object.
Timer(double interval)
Initializes a new instance of the T:System.Timers.Timer class, and sets the P:System....
Definition: Timer.cs:238
Provides constants and static methods for trigonometric, logarithmic, and other common mathematical f...
Definition: Math.cs:10
Provides functionality required by sites.
Definition: ISite.cs:7
Specifies that this object supports a simple, transacted notification for batch initialization.
static decimal Ceiling(decimal d)
Returns the smallest integral value that is greater than or equal to the specified decimal number.
Definition: Math.cs:86
ISynchronizeInvoke SynchronizingObject
Gets or sets the object used to marshal event-handler calls that are issued when an interval has elap...
Definition: Timer.cs:186