แก้ไข

แชร์ผ่าน


TimeProvider.CreateTimer(TimerCallback, Object, TimeSpan, TimeSpan) Method

Definition

Creates a new ITimer instance, using TimeSpan values to measure time intervals.

public:
 virtual System::Threading::ITimer ^ CreateTimer(System::Threading::TimerCallback ^ callback, System::Object ^ state, TimeSpan dueTime, TimeSpan period);
public virtual System.Threading.ITimer CreateTimer (System.Threading.TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period);
abstract member CreateTimer : System.Threading.TimerCallback * obj * TimeSpan * TimeSpan -> System.Threading.ITimer
override this.CreateTimer : System.Threading.TimerCallback * obj * TimeSpan * TimeSpan -> System.Threading.ITimer
Public Overridable Function CreateTimer (callback As TimerCallback, state As Object, dueTime As TimeSpan, period As TimeSpan) As ITimer

Parameters

callback
TimerCallback

A delegate representing a method to be executed when the timer fires. The method specified for callback should be reentrant, as it may be invoked simultaneously on two threads if the timer fires again before or while a previous callback is still being handled.

state
Object

An object to be passed to the callback. This may be null.

dueTime
TimeSpan

The amount of time to delay before callback is invoked. Specify InfiniteTimeSpan to prevent the timer from starting. Specify Zero to start the timer immediately.

period
TimeSpan

The time interval between invocations of callback. Specify InfiniteTimeSpan to disable periodic signaling.

Returns

The newly created ITimer instance.

Exceptions

callback is null.

The number of milliseconds in the value of dueTime or period is negative and not equal to Infinite, or is greater than MaxValue.

Remarks

The delegate specified by the callback parameter is invoked once after dueTime elapses, and thereafter each time the period time interval elapses.

If dueTime is zero, the callback is invoked immediately. If dueTime is -1 milliseconds, callback is not invoked; the timer is disabled, but can be re-enabled by calling the Change(TimeSpan, TimeSpan) method.

If period is 0 or -1 milliseconds and dueTime is positive, callback is invoked once; the periodic behavior of the timer is disabled, but can be re-enabled using the Change(TimeSpan, TimeSpan) method.

The return ITimer instance will be implicitly rooted while the timer is still scheduled.

CreateTimer(TimerCallback, Object, TimeSpan, TimeSpan) captures the ExecutionContext and stores that with the ITimer for use in invoking callback each time it's called. That capture can be suppressed with SuppressFlow().

Applies to