Thread​Pool​Timer Thread​Pool​Timer Thread​Pool​Timer Thread​Pool​Timer Class

Definition

Represents a timer created with CreateTimer or CreatePeriodicTimer.

Note

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

public : sealed class ThreadPoolTimer : IThreadPoolTimerpublic sealed class ThreadPoolTimer : IThreadPoolTimerPublic NotInheritable Class ThreadPoolTimer Implements IThreadPoolTimer// This API is not available in Javascript.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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();
}

Remarks

The CreatePeriodicTimer or CreateTimer 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.

Properties

Delay Delay Delay Delay

Gets the timeout value of a single-use timer created with CreateTimer.

public : TimeSpan Delay { get; }public TimeSpan Delay { get; }Public ReadOnly Property Delay As TimeSpan// This API is not available in Javascript.
Value
TimeSpan TimeSpan TimeSpan TimeSpan

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 Windows::Foundation::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.

public : TimeSpan Period { get; }public TimeSpan Period { get; }Public ReadOnly Property Period As TimeSpan// This API is not available in Javascript.
Value
TimeSpan TimeSpan TimeSpan TimeSpan

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 Windows::Foundation::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 void// This API is not available in Javascript.

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 ThreadPoolTimer// This API is not available in Javascript.
Parameters
handler
TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler

The method to call when the timer expires.

period
TimeSpan TimeSpan TimeSpan TimeSpan

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 Windows::Foundation::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
See Also

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 ThreadPoolTimer// This API is not available in Javascript.
Parameters
handler
TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler

The method to call when the timer expires.

period
TimeSpan TimeSpan TimeSpan TimeSpan

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 Windows::Foundation::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.

destroyed
TimerDestroyedHandler TimerDestroyedHandler TimerDestroyedHandler TimerDestroyedHandler

The method to call after the periodic timer is complete.

Returns
See Also

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 ThreadPoolTimer// This API is not available in Javascript.
Parameters
handler
TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler

The method to call when the timer expires.

delay
TimeSpan TimeSpan TimeSpan TimeSpan

The amount of time until the timer expires.

C++ This amount of time is specified in ticks (100-nanosecond units) using the Windows::Foundation::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

An instance of a single-use timer.

See Also

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 ThreadPoolTimer// This API is not available in Javascript.
Parameters
handler
TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler TimerElapsedHandler

The method to call when the timer expires.

delay
TimeSpan TimeSpan TimeSpan TimeSpan

The amount of time until the timer expires.

C++ This amount of time is specified in ticks (100-nanosecond units) using the Windows::Foundation::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.

destroyed
TimerDestroyedHandler TimerDestroyedHandler TimerDestroyedHandler TimerDestroyedHandler

The method to call after the timer is complete.

Returns

An instance of a single-use timer.

See Also