ThreadPoolTimer 類別

定義

代表使用 CreateTimerCreatePeriodicTimer 建立的計時器。

注意

桌面和 UWP 應用程式都支援 ThreadPool API。

public ref class ThreadPoolTimer sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class ThreadPoolTimer final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class ThreadPoolTimer
Public NotInheritable Class ThreadPoolTimer
繼承
Object Platform::Object IInspectable ThreadPoolTimer
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

下列程式碼示範透過傳入 TimerElapsedHandler 委派方法來建立定期計時器。

int period = 1000;

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

當您的應用程式使用計時器完成時,應該取消它。 下列程式碼會取消上一個範例中建立的定期計時器。

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

備註

CreatePeriodicTimerCreateTimer方法可用來建立這個物件。

注意

timeSpan值為零 (或任何小於 1 毫秒的值,) 會導致定期計時器以單次計時器的形式運作。

屬性

Delay

取得使用 CreateTimer建立之單一使用計時器的逾時值。

Period

取得使用 CreatePeriodicTimer建立的定期計時器逾時值。

方法

Cancel()

取消計時器。

CreatePeriodicTimer(TimerElapsedHandler, TimeSpan)

建立定期計時器。

CreatePeriodicTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)

建立定期計時器,並指定要在定期計時器完成之後呼叫的方法。 當計時器已過期而不重新啟用時,就會完成定期計時器,而 處理常式 的最終呼叫已完成。

CreateTimer(TimerElapsedHandler, TimeSpan)

建立單一使用計時器。

CreateTimer(TimerElapsedHandler, TimeSpan, TimerDestroyedHandler)

建立單一使用計時器,並指定要在計時器完成之後呼叫的方法。 計時器已過期,而 處理常式 的最終呼叫已完成時,就會完成計時器。

適用於