ThreadPoolTimer class

Applies to Windows and Windows Phone

Represents a timer created with CreateTimer or CreatePeriodicTimer.

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

Syntax

Public NotInheritable Class ThreadPoolTimer

Attributes

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]
[WebHostHidden()]

Members

The ThreadPoolTimer class has these types of members:

Methods

The ThreadPoolTimer class has these methods. It also inherits methods from the Object class.

MethodDescription
Cancel Cancels a timer.
CreatePeriodicTimer(TimerElapsedHandler, TimeSpan) Creates a periodic timer.
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.
CreateTimer(TimerElapsedHandler, TimeSpan) Creates a single-use timer.
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.

 

Properties

The ThreadPoolTimer class has these properties.

PropertyAccess typeDescription

Delay

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

Period

Read-onlyGets the timeout value of a periodic timer created with CreatePeriodicTimer.

 

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.

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

Requirements

Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8

Namespace

Windows.System.Threading
Windows::System::Threading [C++]

Metadata

Windows.winmd

 

 

Show:
© 2014 Microsoft