Bagikan melalui


Timer Constructor (TimerCallback)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub New ( _
    callback As TimerCallback _
)
[SecuritySafeCriticalAttribute]
public Timer(
    TimerCallback callback
)

Parameters

Remarks

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.

Examples

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.
using System;
using System.Threading;

public class Example
{
    private static System.Windows.Controls.TextBlock outputBlock;

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Example.outputBlock = outputBlock;
        outputBlock.Text += "Refresh the page to run the example again.\n";

        // Create an instance of the Example class, and start two
        // timers.
        Example ex = new Example();
        ex.StartTimer(2000);
        ex.StartTimer(1000);
    }

    public void StartTimer(int dueTime)
    {
        Timer t = new Timer(TimerProc);
        t.Change(dueTime, 0);
    }

    // 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 void TimerProc(object state)
    {
        // The state object is the Timer object.
        Timer t = (Timer) state;
        t.Dispose();

        Example.outputBlock.Dispatcher.BeginInvoke(delegate () { 
               Example.outputBlock.Text += "Timer callback executes.\n"; });
    }
}

/* This example produces the following output:

Refresh the page to run the example again.
Timer callback executes.
Timer callback executes.
 */

Version Information

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

Platforms

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