Timer::Change Method (Int32, Int32)


Changes the start time and the interval between method invocations for a timer, using 32-bit signed integers to measure time intervals.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

bool Change(
	int dueTime,
	int period


Type: System::Int32

The amount of time to delay before the invoking the callback method specified when the Timer was constructed, in milliseconds. Specify Timeout::Infinite to prevent the timer from restarting. Specify zero (0) to restart the timer immediately.

Type: System::Int32

The time interval between invocations of the callback method specified when the Timer was constructed, in milliseconds. Specify Timeout::Infinite to disable periodic signaling.

Return Value

Type: System::Boolean

true if the timer was successfully updated; otherwise, false.

Exception Condition

The Timer has already been disposed.


The dueTime or period parameter is negative and is not equal to Timeout::Infinite.

The callback method is invoked once after dueTime elapses, and thereafter each time the time interval specified by period elapses.

If dueTime is zero (0), the callback method is invoked immediately. If dueTime is Timeout::Infinite, the callback method is never invoked; the timer is disabled, but can be re-enabled by calling Change and specifying a positive value for dueTime.

If period is zero (0) or Timeout::Infinite, and dueTime is not Timeout::Infinite, the callback method is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled by calling Change and specifying a positive value for period.

The Change method can be called from the TimerCallback delegate.

The following code example demonstrates how to start a Timer and, after a set number of invocations, change its period.

using namespace System;
using namespace System::Threading;

ref class StatusChecker
    int invokeCount, maxCount;

    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;

ref class TimerExample
    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",
        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);
        Console::WriteLine("\nDestroying timer.");

int 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.

Universal Windows Platform
Available since 8.1
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top