Timer.Change Method (TimeSpan, TimeSpan)

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

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

'Declaration
Public Function Change ( _
	dueTime As TimeSpan, _
	period As TimeSpan _
) As Boolean

Parameters

dueTime
Type: System.TimeSpan
The amount of time to delay before invoking the callback method that was specified when the Timer was constructed. Specify -1 (negative one) milliseconds to prevent the timer from restarting. Specify 0 (zero) to restart the timer immediately.
period
Type: System.TimeSpan
The time interval between invocations of the callback method that was specified when the Timer was constructed. Specify -1 (negative one) milliseconds 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 -1 (negative one) milliseconds.

-or-

The dueTime or period parameter is greater than 4294967294 (UInt32.MaxValue - 1) milliseconds.

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

If dueTime is 0 (zero), the callback method is invoked immediately. If dueTime is -1 (negative one) milliseconds, 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 0 (zero) or -1 (negative one) milliseconds, 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 following example demonstrates how to start a Timer and, after a number of invocations, change its period.

This example creates a timer, uses the Timer.Change method to change its interval, and then uses the Timer.Dispose method to destroy it.

The example displays its output in a TextBlock on the UI thread. To access the TextBlock from the callback thread, the example uses the Dispatcher property to obtain a Dispatcher object for the TextBlock, and then uses the Dispatcher.BeginInvoke method to make the cross-thread call.


Imports System.Threading

' The following Imports are not required for the timer. They merely simplify 
' the code.
Imports System.Windows.Controls
Imports System.Windows.Input

Public Class Example

   ' The Shared Demo method sets the starting message and hooks up the handler
   ' for the MouseLeftButtonUp event, which controls the demo.
   Public Shared Sub Demo(ByVal outputBlock As TextBlock)

      outputBlock.Text &= "Click to create the timer." & vbLf
      Example.outputBlock = outputBlock
      AddHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp

   End Sub

   ' Data for the demo.
   Private Shared phase As Integer = 0
   Private Shared t As Timer
   Private Shared outputBlock As TextBlock

   Private Shared Sub MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

      If phase = 0 Then
         ' On the first click, create the timer.
         outputBlock.Text &= vbLf & "Creating the timer at " & _
            DateTime.Now.ToString("h:mm:ss.fff") & _
            ", to start in 1 second with a half-second interval." & vbLf & _
            "Click to change the interval from 1/2 second to 1 second." & vbLf & vbLf

         ' Create a timer that invokes the callback method after one second 
         ' and every 1/2 second (500 milliseconds) thereafter. Visual Basic 
         ' infers the delegate type, as if you had typed the following:
         '     New TimerCallback(AddressOf MyTimerCallback)
         ' 
         t = New Timer(AddressOf MyTimerCallback, Nothing, _
                       New TimeSpan(0, 0, 1), New TimeSpan(0, 0, 0, 0, 500))

      ElseIf phase = 1 Then
         ' On the next click, change the timer interval to every second.
         outputBlock.Text &= vbLf & "Changing the interval to one second." & vbLf & _
            "Click to destroy the timer." & vbLf & vbLf

         t.Change(New TimeSpan(0), New TimeSpan(0, 0, 1))

      Else
         ' On the last click, destroy the timer and shut down the demo.
         outputBlock.Text &= vbLf & "Destroying the timer." & vbLf & _
            "Refresh the page to run the demo again."
         RemoveHandler outputBlock.MouseLeftButtonUp, AddressOf MouseUp

         t.Dispose()

      End If

      phase += 1

   End Sub

   ' The shared callback method is invoked on a ThreadPool thread by the Timer. In 
   ' this example, the state object is not used. In order to update the TextBlock 
   ' object, which is on the UI thread, you must make the cross-thread call by using 
   ' the Dispatcher object that is associated with the TextBlock.
   Private Shared Sub MyTimerCallback(ByVal state As Object)

      Dim msg As String = DateTime.Now.ToString("h:mm:ss.fff") & _
                          " MyTimerCallback was called." & vbLf

      outputBlock.Dispatcher.BeginInvoke(displayHelper, msg)

   End Sub

   ' The DisplayOutput helper method and its delegate, displayHelper, are used by
   ' the BeginInvoke method of the Dispatcher object.
   Private Shared displayHelper As New Action(Of String)(AddressOf DisplayOutput)
   Private Shared Sub DisplayOutput(ByVal msg As String)
      outputBlock.Text &= msg
   End Sub

End Class

' This example produces output similar to the following:
'
'Click to create the timer.
'
'Creating the timer at 4:14:48.114, to start in 1 second with a half-second interval.
'Click to change the interval from 1/2 second to 1 second.
'
'4:14:49.221 MyTimerCallback was called.
'4:14:49.736 MyTimerCallback was called.
'4:14:50.211 MyTimerCallback was called.
'
'Changing the interval to one second.
'Click to destroy the timer.
'
'4:14:50.640 MyTimerCallback was called.
'4:14:51.676 MyTimerCallback was called.
'
'Destroying the timer.
'Refresh the page to run the demo again.


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show: