Class TimerFactory
Provides access to factory methods for creating and configuring Timer instances.
public static class TimerFactory
- Inheritance
-
TimerFactory
Examples
The following example creates a non-capturing timer that fires every two seconds after an initial one-second delay. The callback prints "Timer ticked" each time it executes; the timer is disposed after five seconds.
using System;
using System.Threading;
using Cuemon.Threading;
namespace Cuemon.Threading;
public class TimerFactoryExample
{
public void Demonstrate()
{
using var timer = TimerFactory.CreateNonCapturingTimer(
state => Console.WriteLine("Timer ticked"),
null,
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(2));
Console.WriteLine("Timer started (1s delay, 2s interval)");
Thread.Sleep(5000);
}
}
Methods
CreateNonCapturingTimer(TimerCallback, object, TimeSpan, TimeSpan)
Initializes a new instance of the Timer class that suppress capturing the ExecutionContext.
public static Timer CreateNonCapturingTimer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
Parameters
callbackTimerCallbackA delegate representing a method to be executed.
stateobjectAn object containing information to be used by the callback method, or
null.dueTimeTimeSpanThe amount of time to delay before the callback is invoked. Specify InfiniteTimeSpan to prevent the timer from starting. Specify Zero to start the timer immediately.
periodTimeSpanThe time interval between invocations of callback. Specify InfiniteTimeSpan to disable periodic signaling.
Returns
Remarks
Used by Microsoft internally in various scenarios: https://github.com/dotnet/runtime/blob/master/src/libraries/Common/src/Extensions/NonCapturingTimer/NonCapturingTimer.cs