Skip to content

A Very Simple Multimedia Timer with High Precision / High Resolution Events accurate to 1 Millisecond without destroying your CPU

License

Notifications You must be signed in to change notification settings

anloganlog/Precision-Timer.NET

Repository files navigation

PrecisionTimer .NET

Nuget

This timer is as precise as the platform you are running it on down to 1 millisecond.

PrecisionTimer.NET won't randomly dispose itself, You don't need to keep a special reference to it.

If you intend to use a lot of timers or you want to use PrecisionTimer.NET to repeat a long running task, consider using Timer Sink.NET

Basic Usage

using PrecisionTiming;
public static PrecisionTimer MyTimer = new();

Using Precision Timer is as simple as using any other Windows Timer.

MyTimer.SetInterval(SomeAction, Interval);

Start the Timer

MyTimer.Start();

Check if the Timer is Running

MyTimer.IsRunning();

Stop the Timer

MyTimer.Stop();

Dispose the Timer

MyTimer.Dispose();

SetInterval / Configure

If you use SetInterval to set only Action and Interval the timer will automatically start with the most common defaults, but it has several optional parameters you can change

For example, If you don't want the timer to start automatically then use the following

MyTimer.SetInterval(SomeAction, Interval, false);

You can use Configure instead of SetInterval if you prefer

MyTimer.Configure(SomeAction, Interval, false);

Manual Setup

You can manually configure the Timer and then Start it yourself instead of using SetInterval

SetAction

Sets the Action that will be triggered by the TimerCallback when the Period has elapsed

MyTimer.SetAction(Action);

Periodic Timers must stop before setting.

SetPeriod / SetInterval(int)

Sets the Period (Interval) between Actions in Milliseconds.

MyTimer.SetPeriod(int);
MyTimer.GetPeriod();

Both Timer Modes must stop before setting.

SetResolution

Set the Resolution of the Timer

MyTimer.SetResolution(int);
MyTimer.GetResolution();

The resolution is in milliseconds, The default resolution for SetInterval(Action) is 0

The Resolution increases with smaller values.

A resolution of 0 indicates periodic events should occur with the greatest possible accuracy.

To reduce system overhead, however, you should use the maximum value appropriate for your application.

The normal Resolution of a .Net Timer is around 12-15ms

Both Timer Modes must stop before setting.

SetEventArgs

Set EventArgs of the Timer

MyTimer.SetEventArgs(EventArgs);
MyTimer.GetEventArgs();

Periodic Timers must stop before setting.

SetAutoResetMode

Set the Periodic/OneShot Mode of the

MyTimer.SetAutoResetMode(bool);
MyTimer.GetAutoResetMode();

True if the PrecisionTimer should raise the Ticks Event each time the interval elapses. (Periodic)

False if the PrecisionTimer should raise the event only once AFTER the first time the interval elapses. (One-Shot)

Both Timer Modes must stop before setting.

Timing Settings

Global Multimedia Timer settings that affect your application

Set the Applications Minimum Resolution

PrecisionTimerSettings.SetMinimumTimerResolution(0);

Clear the Applications Minimum Resolution

PrecisionTimerSettings.ClearMinimumTimerResolution(0);

Multimedia Timer

Precision Timer.NET is a Multimedia Timer.

You can read more about Multimedia Timers on MSDN

Consider using Timer Sink.NET instead of using PrecisionTimer.NET directly

About

A Very Simple Multimedia Timer with High Precision / High Resolution Events accurate to 1 Millisecond without destroying your CPU

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages