TimerCallback Delegate
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Represents the method that handles calls from a Timer.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- state
- Type: System.Object
An object that contains application-specific information that is relevant to the method invoked by this delegate.
-or-
A null reference (Nothing in Visual Basic).
Use a TimerCallback delegate to specify the method that is called by a Timer. This method does not execute in the thread that created the timer; it executes in a separate thread pool thread that is provided by the system. The TimerCallback delegate invokes the method once after the start time elapses, and continues to invoke it once per timer interval until the Dispose method is called, or until the Timer.Change method is called with the interval value Infinite.
The timer delegate is specified when the timer is constructed, and cannot be changed. The start time for a Timer is passed in the dueTime parameter of the Timer constructors, and the period is passed in the period parameter.
The following example shows how to create the delegate that is used with the Timer class. In this example, the delegate is created implicitly. Its type is inferred by the Visual Basic and C# compilers.
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.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
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 ' The Example class holds a reference to the timer, and contains the ' event handler for the MouseLeftButtonUp events that control the demo. ' Public Class Example ' The Shared Demo method sets the starting message and creates an ' instance of Example, which hooks up the handler for the MouseLeftButtonUp ' event. Public Shared Sub Demo(ByVal outputBlock As TextBlock) outputBlock.Text &= "Click to create the timer." & vbLf Dim dummy As New Example(outputBlock) End Sub ' Instance data for the demo. Private phase As Integer = 0 Private t As Timer Public Sub New(ByVal outputBlock As TextBlock) ' Hook up the mouse event when a new Example object is created. Note ' that this keeps garbage collection from reclaiming the Example ' object. AddHandler outputBlock.MouseLeftButtonUp, AddressOf Me.MouseUp End Sub Private Sub MouseUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Dim outputBlock As TextBlock = CType(sender, TextBlock) 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 ' (1000 milliseconds) and every 1/2 second thereafter. The TextBlock ' that is used for output is passed as the state object. Visual Basic ' infers the delegate type, as if you had typed the following: ' New TimerCallback(AddressOf MyTimerCallback) ' t = New Timer(AddressOf MyTimerCallback, outputBlock, 1000, 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(0, 1000) 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 Me.MouseUp t.Dispose() End If phase += 1 End Sub ' The shared callback method is invoked on a ThreadPool thread by the Timer. ' The state object is passed to the callback method on each invocation. In this ' example, the state object is the TextBlock that displays output. 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 outputBlock As TextBlock = CType(state, TextBlock) Dim msg As String = DateTime.Now.ToString("h:mm:ss.fff") & _ " MyTimerCallback was called." & vbLf outputBlock.Dispatcher.BeginInvoke(displayHelper, outputBlock, 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 TextBlock, String)(AddressOf DisplayOutput) Private Shared Sub DisplayOutput(ByVal tb As TextBlock, ByVal msg As String) tb.Text &= msg End Sub End Class ' This example produces output similar to the following: ' 'Click to create the timer. ' 'Creating the timer at 3:17:36.980, to start in 1 second with a half-second interval. 'Click to change the interval from 1/2 second to 1 second. ' '3:17:38.072 MyTimerCallback was called. '3:17:38.586 MyTimerCallback was called. '3:17:39.101 MyTimerCallback was called. '3:17:39.580 MyTimerCallback was called. ' 'Changing the interval to one second. 'Click to destroy the timer. ' '3:17:39.656 MyTimerCallback was called. '3:17:40.689 MyTimerCallback was called. ' 'Destroying the timer. 'Refresh the page to run the demo again.
Note: