Timer Constructor (TimerCallback)

Timer Constructor (TimerCallback)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Initializes a new instance of the Timer class with an infinite interval and an infinite due time, using the newly created Timer object as the state object.

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

'Declaration
Public Sub New ( _
	callback As TimerCallback _
)

Parameters

callback
Type: System.Threading.TimerCallback
A delegate that represents a method to be executed.

Call this constructor when you want to use the Timer object itself as the state object. After creating the timer, use the Change method to set the interval and due time.

This constructor specifies an infinite due time before the first callback and an infinite interval between callbacks, in order to prevent the first callback from occurring before the Timer object is assigned to the state object.

The method specified for callback should be reentrant, because it is called on ThreadPool threads. The method can be executed simultaneously on two thread pool threads if the timer interval is less than the time required to execute the method, or if all thread pool threads are in use and the method is queued multiple times.

The following example creates a new timer, using the timer itself as the state object. The Change method is used to start the timer. When the timer callback occurs, the state object is used to turn the timer off.

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

Public Class Example

    Private Shared outputBlock As System.Windows.Controls.TextBlock

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Example.outputBlock = outputBlock
        outputBlock.Text &= "Refresh the page to run the example again." & vbLf

        ' Create an instance of the Example class, and start two
        ' timers.
        Dim ex As New Example()
        ex.StartTimer(2000)
        ex.StartTimer(1000)
    End Sub

    Public Sub StartTimer(ByVal dueTime As Integer)
        Dim t As New Timer(AddressOf TimerProc)
        t.Change(dueTime, 0)
    End Sub

    ' The callback method is invoked on a ThreadPool thread by the Timer. 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 Sub TimerProc(ByVal state As Object)
        ' The state object is the Timer object.
        Dim t As Timer = CType(state, Timer)
        t.Dispose()

        Example.outputBlock.Dispatcher.BeginInvoke(displayHelper, _
                                                   "Timer callback executes." & vbLf)
    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)
        Example.outputBlock.Text &= msg
    End Sub

End Class

' This example produces the following output:
'
'Refresh the page to run the example again.
'Timer callback executes.
'Timer callback executes.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft