mscorlib(4.0.0.0) API with additions
Timer.cs
3 using System.Security;
5 
6 namespace System.Threading
7 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
11  [HostProtection(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
12  public sealed class Timer : MarshalByRefObject, IDisposable
13  {
14  private const uint MAX_SUPPORTED_TIMEOUT = 4294967294u;
15 
16  private TimerHolder m_timer;
17 
25  [MethodImpl(MethodImplOptions.NoInlining)]
26  [SecuritySafeCritical]
27  [__DynamicallyInvokable]
28  public Timer(TimerCallback callback, object state, int dueTime, int period)
29  {
30  if (dueTime < -1)
31  {
32  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
33  }
34  if (period < -1)
35  {
36  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
37  }
38  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
39  TimerSetup(callback, state, (uint)dueTime, (uint)period, ref stackMark);
40  }
41 
49  [MethodImpl(MethodImplOptions.NoInlining)]
50  [SecuritySafeCritical]
51  [__DynamicallyInvokable]
52  public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
53  {
54  long num = (long)dueTime.TotalMilliseconds;
55  if (num < -1)
56  {
57  throw new ArgumentOutOfRangeException("dueTm", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
58  }
59  if (num > 4294967294u)
60  {
61  throw new ArgumentOutOfRangeException("dueTm", Environment.GetResourceString("ArgumentOutOfRange_TimeoutTooLarge"));
62  }
63  long num2 = (long)period.TotalMilliseconds;
64  if (num2 < -1)
65  {
66  throw new ArgumentOutOfRangeException("periodTm", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
67  }
68  if (num2 > 4294967294u)
69  {
70  throw new ArgumentOutOfRangeException("periodTm", Environment.GetResourceString("ArgumentOutOfRange_PeriodTooLarge"));
71  }
72  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
73  TimerSetup(callback, state, (uint)num, (uint)num2, ref stackMark);
74  }
75 
83  [MethodImpl(MethodImplOptions.NoInlining)]
84  [CLSCompliant(false)]
85  [SecuritySafeCritical]
86  public Timer(TimerCallback callback, object state, uint dueTime, uint period)
87  {
88  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
89  TimerSetup(callback, state, dueTime, period, ref stackMark);
90  }
91 
99  [MethodImpl(MethodImplOptions.NoInlining)]
100  [SecuritySafeCritical]
101  public Timer(TimerCallback callback, object state, long dueTime, long period)
102  {
103  if (dueTime < -1)
104  {
105  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
106  }
107  if (period < -1)
108  {
109  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
110  }
111  if (dueTime > 4294967294u)
112  {
113  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_TimeoutTooLarge"));
114  }
115  if (period > 4294967294u)
116  {
117  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_PeriodTooLarge"));
118  }
119  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
120  TimerSetup(callback, state, (uint)dueTime, (uint)period, ref stackMark);
121  }
122 
125  [MethodImpl(MethodImplOptions.NoInlining)]
126  [SecuritySafeCritical]
127  public Timer(TimerCallback callback)
128  {
129  int dueTime = -1;
130  int period = -1;
131  StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
132  TimerSetup(callback, this, (uint)dueTime, (uint)period, ref stackMark);
133  }
134 
135  [SecurityCritical]
136  private void TimerSetup(TimerCallback callback, object state, uint dueTime, uint period, ref StackCrawlMark stackMark)
137  {
138  if (callback == null)
139  {
140  throw new ArgumentNullException("TimerCallback");
141  }
142  m_timer = new TimerHolder(new TimerQueueTimer(callback, state, dueTime, period, ref stackMark));
143  }
144 
145  [SecurityCritical]
146  internal static void Pause()
147  {
148  TimerQueue.Instance.Pause();
149  }
150 
151  [SecurityCritical]
152  internal static void Resume()
153  {
154  TimerQueue.Instance.Resume();
155  }
156 
164  [__DynamicallyInvokable]
165  public bool Change(int dueTime, int period)
166  {
167  if (dueTime < -1)
168  {
169  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
170  }
171  if (period < -1)
172  {
173  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
174  }
175  return m_timer.m_timer.Change((uint)dueTime, (uint)period);
176  }
177 
186  [__DynamicallyInvokable]
187  public bool Change(TimeSpan dueTime, TimeSpan period)
188  {
189  return Change((long)dueTime.TotalMilliseconds, (long)period.TotalMilliseconds);
190  }
191 
198  [CLSCompliant(false)]
199  public bool Change(uint dueTime, uint period)
200  {
201  return m_timer.m_timer.Change(dueTime, period);
202  }
203 
212  public bool Change(long dueTime, long period)
213  {
214  if (dueTime < -1)
215  {
216  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
217  }
218  if (period < -1)
219  {
220  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegOrNegative1"));
221  }
222  if (dueTime > 4294967294u)
223  {
224  throw new ArgumentOutOfRangeException("dueTime", Environment.GetResourceString("ArgumentOutOfRange_TimeoutTooLarge"));
225  }
226  if (period > 4294967294u)
227  {
228  throw new ArgumentOutOfRangeException("period", Environment.GetResourceString("ArgumentOutOfRange_PeriodTooLarge"));
229  }
230  return m_timer.m_timer.Change((uint)dueTime, (uint)period);
231  }
232 
238  public bool Dispose(WaitHandle notifyObject)
239  {
240  if (notifyObject == null)
241  {
242  throw new ArgumentNullException("notifyObject");
243  }
244  return m_timer.Close(notifyObject);
245  }
246 
248  [__DynamicallyInvokable]
249  public void Dispose()
250  {
251  m_timer.Close();
252  }
253 
254  internal void KeepRootedWhileScheduled()
255  {
256  GC.SuppressFinalize(m_timer);
257  }
258  }
259 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Encapsulates operating system–specific objects that wait for exclusive access to shared resources.
Definition: WaitHandle.cs:15
bool Change(uint dueTime, uint period)
Changes the start time and the interval between method invocations for a timer, using 32-bit unsigned...
Definition: Timer.cs:199
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Timer(TimerCallback callback, object state, long dueTime, long period)
Initializes a new instance of the Timer class, using 64-bit signed integers to measure time intervals...
Definition: Timer.cs:101
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
Definition: TimeSpan.cs:180
Timer(TimerCallback callback, object state, int dueTime, int period)
Initializes a new instance of the Timer class, using a 32-bit signed integer to specify the time inte...
Definition: Timer.cs:28
void Dispose()
Releases all resources used by the current instance of T:System.Threading.Timer.
Definition: Timer.cs:249
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool Change(long dueTime, long period)
Changes the start time and the interval between method invocations for a timer, using 64-bit signed i...
Definition: Timer.cs:212
Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
Initializes a new instance of the Timer class, using T:System.TimeSpan values to measure time interva...
Definition: Timer.cs:52
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
MethodImplOptions
Defines the details of how a method is implemented.
Timer(TimerCallback callback)
Initializes a new instance of the T:System.Threading.Timer class with an infinite period and an infin...
Definition: Timer.cs:127
bool Dispose(WaitHandle notifyObject)
Releases all resources used by the current instance of T:System.Threading.Timer and signals when the ...
Definition: Timer.cs:238
Timer(TimerCallback callback, object state, uint dueTime, uint period)
Initializes a new instance of the Timer class, using 32-bit unsigned integers to measure time interva...
Definition: Timer.cs:86
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
bool Change(int dueTime, int period)
Changes the start time and the interval between method invocations for a timer, using 32-bit signed i...
Definition: Timer.cs:165
bool Change(TimeSpan dueTime, TimeSpan period)
Changes the start time and the interval between method invocations for a timer, using T:System....
Definition: Timer.cs:187
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
Enables access to objects across application domain boundaries in applications that support remoting.