ThreadPoolTimer ThreadPoolTimer ThreadPoolTimer Class

Represents a timer created with CreateTimer(TimerElapsedHandler, TimeSpan) or CreatePeriodicTimer(TimerElapsedHandler, TimeSpan).

Note

The ThreadPool API is supported for desktop as well as Windows Store apps.

Syntax

Declaration

public sealed class ThreadPoolTimerpublic sealed class ThreadPoolTimerPublic NotInheritable Class ThreadPoolTimer

Remarks

The CreatePeriodicTimer(TimerElapsedHandler, TimeSpan) or CreateTimer(TimerElapsedHandler, TimeSpan) method can be used to create this object.

Note

A TimeSpan value of zero (or any value less than 1 millisecond) will cause the periodic timer to behave as a single-shot timer.

Examples

The following code shows the creation of a periodic timer by passing in a TimerElapsedHandler delegate method. The delegate method should periodically check the Canceled property of the operation parameter, and exit gracefully if the work item has been cancelled.

int period = 1000;

ThreadPoolTimer PeriodicTimer = 
    ThreadPoolTimer.CreatePeriodicTimer(ExampleTimerElapsedHandler,
                                        TimeSpan.FromMilliseconds(period));

When your app is done using the timer, it should be cancelled. The following code cancels the periodic timer created in the previous example.

if (PeriodicTimer != null)
{
    PeriodicTimer.Cancel();
}

Properties summary

Gets the timeout value of a single-use timer created with CreateTimer(TimerElapsedHandler, TimeSpan).

Gets the timeout value of a periodic timer created with CreatePeriodicTimer(TimerElapsedHandler, TimeSpan).

Methods summary

Cancels a timer.

Creates a periodic timer.

Creates a periodic timer and specifies a method to call after the periodic timer is complete. The periodic timer is complete when the timer has expired without being reactivated, and the final call to handler has finished.

Creates a single-use timer.

Creates a single-use timer and specifies a method to call after the timer is complete. The timer is complete when the timer has expired and the final call to handler has finished.

Properties

  • Delay
    Delay
    Delay
    Delay

    Gets the timeout value of a single-use timer created with CreateTimer(TimerElapsedHandler, TimeSpan).

    public TimeSpan Delay { get; }public TimeSpan Delay { get; }Public ReadOnly Property Delay As TimeSpanpublic TimeSpan Delay { get; }

    Property Value

    • The timeout value. When the timeout value elapses, the timer expires and its TimerElapsedHandler delegate is called.

      C++ This amount of time is specified in ticks (100-nanosecond units) using the TimeSpan structure.

      JavaScript and C# This amount of time is specified using the System.TimeSpan structure. It can be specified in ticks, or it can be specified in milliseconds, seconds, and so on.

    Remarks

    A timer begins counting down as soon as the timer object is created.

  • Period
    Period
    Period
    Period

    Gets the timeout value of a periodic timer created with CreatePeriodicTimer(TimerElapsedHandler, TimeSpan).

    public TimeSpan Period { get; }public TimeSpan Period { get; }Public ReadOnly Property Period As TimeSpanpublic TimeSpan Period { get; }

    Property Value

    • The timeout value. When the timeout value elapses, the timer expires, its TimerElapsedHandler delegate is called, and the timer reactivates. This behavior continues until the timer is canceled.

      C++ This amount of time is specified in ticks (100-nanosecond units) using the TimeSpan structure.

      JavaScript and C# This amount of time is specified using the System.TimeSpan structure. It can be specified in ticks, or it can be specified in milliseconds, seconds, and so on.

    Remarks

    A periodic timer begins counting down as soon as the timer object is created. When the timer expires, it is reactivated and begins counting down again.

Methods

  • Cancel()
    Cancel()
    Cancel()
    Cancel()

    Cancels a timer.

    public void Cancel()public void Cancel()Public Function Cancel() As voidpublic void Cancel()

    Remarks

    When a timer is canceled, pending TimerElapsedHandler delegates are also canceled. TimerElapsedHandler delegates that are already running are allowed to finish.

  • CreatePeriodicTimer(TimerElapsedHandler, TimeSpan)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan)

    Creates a periodic timer.

    public static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period)public static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period)Public Static Function CreatePeriodicTimer(handler As TimerElapsedHandler, period As TimeSpan) As ThreadPoolTimerpublic static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period)

    Parameters

    • handler

      The method to call when the timer expires.

    • period

      The amount of time until the timer expires. The timer reactivates each time the period elapses, until the timer is canceled.

      C++ This amount of time is specified in ticks (100-nanosecond units) using the TimeSpan structure.

      JavaScript and C# This amount of time is specified using the System.TimeSpan structure. It can be specified in ticks, or it can be specified in milliseconds, seconds, and so on.

    Returns

  • CreatePeriodicTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreatePeriodicTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)

    Creates a periodic timer and specifies a method to call after the periodic timer is complete. The periodic timer is complete when the timer has expired without being reactivated, and the final call to handler has finished.

    public static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period, TimerDestroyedHandler destroyed)public static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period, TimerDestroyedHandler destroyed)Public Static Function CreatePeriodicTimer(handler As TimerElapsedHandler, period As TimeSpan, destroyed As TimerDestroyedHandler) As ThreadPoolTimerpublic static ThreadPoolTimer CreatePeriodicTimer(TimerElapsedHandler handler, TimeSpan period, TimerDestroyedHandler destroyed)

    Parameters

    Returns

  • CreateTimer(TimerElapsedHandler, TimeSpan)
    CreateTimer(TimerElapsedHandler, TimeSpan)
    CreateTimer(TimerElapsedHandler, TimeSpan)
    CreateTimer(TimerElapsedHandler, TimeSpan)

    Creates a single-use timer.

    public static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay)public static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay)Public Static Function CreateTimer(handler As TimerElapsedHandler, delay As TimeSpan) As ThreadPoolTimerpublic static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay)

    Parameters

    Returns

  • CreateTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreateTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreateTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)
    CreateTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)

    Creates a single-use timer and specifies a method to call after the timer is complete. The timer is complete when the timer has expired and the final call to handler has finished.

    public static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay, TimerDestroyedHandler destroyed)public static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay, TimerDestroyedHandler destroyed)Public Static Function CreateTimer(handler As TimerElapsedHandler, delay As TimeSpan, destroyed As TimerDestroyedHandler) As ThreadPoolTimerpublic static ThreadPoolTimer CreateTimer(TimerElapsedHandler handler, TimeSpan delay, TimerDestroyedHandler destroyed)

    Parameters

    Returns

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.WebHostHiddenAttribute

Details

Assembly

Windows.System.Threading.dll