Timer Constructor (TimerCallback^, Object^, Int32, Int32)
Initializes a new instance of the Timer class, using a 32-bit signed integer to specify the time interval.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- callback
-
Type:
System.Threading::TimerCallback^
A TimerCallback delegate representing a method to be executed.
- state
-
Type:
System::Object^
An object containing information to be used by the callback method, or null.
- dueTime
-
Type:
System::Int32
The amount of time to delay before callback is invoked, in milliseconds. Specify Timeout::Infinite to prevent the timer from starting. Specify zero (0) to start the timer immediately.
- period
-
Type:
System::Int32
The time interval between invocations of callback, in milliseconds. Specify Timeout::Infinite to disable periodic signaling.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | The dueTime or period parameter is negative and is not equal to Timeout::Infinite. |
| ArgumentNullException | The callback parameter is null. |
The delegate specified by the callback parameter is invoked once after dueTime elapses, and thereafter each time the period time interval elapses.
If dueTime is zero (0), callback is invoked immediately. If dueTime is Timeout::Infinite, callback is not invoked; the timer is disabled, but can be re-enabled by calling the Change method.
Because the Timer class has the same resolution as the system clock, which is approximately 15 milliseconds on Windows 7 and Windows 8 systems, the callback delegate executes at intervals defined by the resolution of the system clock if period is less than the resolution of the system clock. If period is zero (0) or Timeout::Infinite and dueTime is not Timeout::Infinite, callback is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled using the Change method.
The method specified for callback should be reentrant, because it is called on ThreadPool threads. The method can be executed simultaneously on two thread pool threads if the timer interval is less than the time required to execute the method, or if all thread pool threads are in use and the method is queued multiple times.
The following code example shows how to create a TimerCallback delegate and initialize a new instance of the Timer class.
using namespace System; using namespace System::Threading; ref class StatusChecker { private: int invokeCount, maxCount; public: StatusChecker(int count) { invokeCount = 0; maxCount = count; } // This method is called by the timer delegate. void CheckStatus(Object^ stateInfo) { AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo); Console::WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.", DateTime::Now, ++invokeCount); if (invokeCount == maxCount) { // Reset the counter and signal the waiting thread. invokeCount = 0; autoEvent->Set(); } } }; ref class TimerExample { public: static void Main() { // Create an AutoResetEvent to signal the timeout threshold in the // timer callback has been reached. AutoResetEvent^ autoEvent = gcnew AutoResetEvent(false); StatusChecker^ statusChecker = gcnew StatusChecker(10); // Create a delegate that invokes methods for the timer. TimerCallback^ tcb = gcnew TimerCallback(statusChecker, &StatusChecker::CheckStatus); // Create a timer that invokes CheckStatus after one second, // and every 1/4 second thereafter. Console::WriteLine("{0:h:mm:ss.fff} Creating timer.\n", DateTime::Now); Timer^ stateTimer = gcnew Timer(tcb, autoEvent, 1000, 250); // When autoEvent signals, change the period to every half second. autoEvent->WaitOne(5000, false); stateTimer->Change(0, 500); Console::WriteLine("\nChanging period to .5 seconds.\n"); // When autoEvent signals the second time, dispose of the timer. autoEvent->WaitOne(5000, false); stateTimer->~Timer(); Console::WriteLine("\nDestroying timer."); } }; int main() { TimerExample::Main(); } // The example displays output like the following: // 11:59:54.202 Creating timer. // // 11:59:55.217 Checking status 1. // 11:59:55.466 Checking status 2. // 11:59:55.716 Checking status 3. // 11:59:55.968 Checking status 4. // 11:59:56.218 Checking status 5. // 11:59:56.470 Checking status 6. // 11:59:56.722 Checking status 7. // 11:59:56.972 Checking status 8. // 11:59:57.223 Checking status 9. // 11:59:57.473 Checking status 10. // // Changing period to .5 seconds. // // 11:59:57.474 Checking status 1. // 11:59:57.976 Checking status 2. // 11:59:58.476 Checking status 3. // 11:59:58.977 Checking status 4. // 11:59:59.477 Checking status 5. // 11:59:59.977 Checking status 6. // 12:00:00.478 Checking status 7. // 12:00:00.980 Checking status 8. // 12:00:01.481 Checking status 9. // 12:00:01.981 Checking status 10. // // Destroying timer.
Available since 8.1
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1