This documentation is archived and is not being maintained.

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)

public:
bool Change(
	int dueTime, 
	int period
)

Parameters

dueTime
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.
period
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.

ExceptionCondition
ObjectDisposedException

The Timer has already been disposed.

ArgumentOutOfRangeException

The dueTime or period parameter is negative and is not equal to 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 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 Infinite, and dueTime is not 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 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
{
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} Checking status {1,2}.",
            DateTime::Now.ToString("h:mm:ss.fff"),
            (++invokeCount).ToString());

        if (invokeCount == maxCount)
        {
            // Reset the counter and signal Main.
            invokeCount  = 0;
            autoEvent->Set();
        }
    }
};

ref class TimerExample
{
public:
    static void Main()
    {
        // Create an event to signal the timeout count threshold in the
        // timer callback.
        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 signals the delegate to invoke
        // CheckStatus after one second, and every 1/4 second 
        // thereafter.
        Console::WriteLine("{0} Creating timer.\n",
            DateTime::Now.ToString("h:mm:ss.fff"));
        Timer^ stateTimer = gcnew Timer(tcb, autoEvent, 1000, 250);

        // When autoEvent signals, change the period to every
        // 1/2 second.
        autoEvent->WaitOne(5000, false);
        stateTimer->Change(0, 500);
        Console::WriteLine("\nChanging period.\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();
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: