Timer.Change Method

Definition

Changes the start time and the interval between method invocations for a timer.

Overloads

Change(Int32, Int32)

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

Change(Int64, Int64)

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

Change(TimeSpan, TimeSpan)

Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals.

Change(UInt32, UInt32)

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

Change(Int32, Int32)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

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

C#
public bool Change(int dueTime, int period);

Parameters

dueTime
Int32

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

period
Int32

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

Returns

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

Exceptions

The Timer has already been disposed.

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

Examples

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

C#
using System;
using System.Threading;

class TimerExample
{
    static void Main()
    {
        // Create an AutoResetEvent to signal the timeout threshold in the
        // timer callback has been reached.
        var autoEvent = new AutoResetEvent(false);
        
        var statusChecker = new StatusChecker(10);

        // 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);
        var stateTimer = new Timer(statusChecker.CheckStatus, 
                                   autoEvent, 1000, 250);

        // When autoEvent signals, change the period to every half second.
        autoEvent.WaitOne();
        stateTimer.Change(0, 500);
        Console.WriteLine("\nChanging period to .5 seconds.\n");

        // When autoEvent signals the second time, dispose of the timer.
        autoEvent.WaitOne();
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
    }
}

class StatusChecker
{
    private int invokeCount;
    private int  maxCount;

    public StatusChecker(int count)
    {
        invokeCount  = 0;
        maxCount = count;
    }

    // This method is called by the timer delegate.
    public void CheckStatus(Object stateInfo)
    {
        AutoResetEvent autoEvent = (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 the waiting thread.
            invokeCount = 0;
            autoEvent.Set();
        }
    }
}
// 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.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Change(Int64, Int64)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

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

C#
public bool Change(long dueTime, long period);

Parameters

dueTime
Int64

The amount of time to delay before the invoking the callback method specified when the Timer was constructed, in milliseconds. Specify Infinite to prevent the timer from restarting. Specify zero (0) to restart the timer immediately. This value must be less than or equal to 4294967294.

period
Int64

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

Returns

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

Exceptions

The Timer has already been disposed.

dueTime or period is less than -1.

-or-

dueTime or period is greater than 4294967294.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Change(TimeSpan, TimeSpan)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

Changes the start time and the interval between method invocations for a timer, using TimeSpan values to measure time intervals.

C#
public bool Change(TimeSpan dueTime, TimeSpan period);

Parameters

dueTime
TimeSpan

A TimeSpan representing the amount of time to delay before invoking the callback method specified when the Timer was constructed. Specify InfiniteTimeSpan to prevent the timer from restarting. Specify Zero to restart the timer immediately.

period
TimeSpan

The time interval between invocations of the callback method specified when the Timer was constructed. Specify InfiniteTimeSpan to disable periodic signaling.

Returns

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

Implements

Exceptions

The Timer has already been disposed.

The dueTime or period parameter, in milliseconds, is less than -1.

The dueTime or period parameter, in milliseconds, is greater than 4294967294.

Examples

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

C#
using System;
using System.Threading;

class TimerExample
{
    static void Main()
    {
        AutoResetEvent autoEvent     = new AutoResetEvent(false);
        StatusChecker  statusChecker = new StatusChecker(10);

        // Create the delegate that invokes methods for the timer.
        TimerCallback timerDelegate = 
            new TimerCallback(statusChecker.CheckStatus);

        TimeSpan delayTime = new TimeSpan(0, 0, 1);
        TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 250);

        // 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 = new Timer(
            timerDelegate, autoEvent, delayTime, intervalTime);

        // When autoEvent signals, change the period to every 
        // 1/2 second.
        autoEvent.WaitOne(5000, false);
        stateTimer.Change(new TimeSpan(0), 
            intervalTime + intervalTime);
        Console.WriteLine("\nChanging period.\n");

        // When autoEvent signals the second time, dispose of 
        // the timer.
        autoEvent.WaitOne(5000, false);
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
    }
}

class StatusChecker
{
    int invokeCount, maxCount;

    public StatusChecker(int count)
    {
        invokeCount  = 0;
        maxCount = count;
    }

    // This method is called by the timer delegate.
    public void CheckStatus(Object stateInfo)
    {
        AutoResetEvent autoEvent = (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();
        }
    }
}

Remarks

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

If dueTime is TimeSpan.Zero, the callback method is invoked immediately. If dueTime is InfiniteTimeSpan, 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 TimeSpan.Zero or InfiniteTimeSpan, and dueTime is positive, 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 value greater than zero for period.

The Change method can be called from the TimerCallback delegate.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Change(UInt32, UInt32)

Source:
Timer.cs
Source:
Timer.cs
Source:
Timer.cs

Important

This API is not CLS-compliant.

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

C#
[System.CLSCompliant(false)]
public bool Change(uint dueTime, uint period);

Parameters

dueTime
UInt32

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

period
UInt32

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

Returns

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

Attributes

Exceptions

The Timer has already been disposed.

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1