BlyZe.BackgroundTimer 1.1.0

.NET 6.0
NuGet\Install-Package BlyZe.BackgroundTimer -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package BlyZe.BackgroundTimer --version 1.1.0
<PackageReference Include="BlyZe.BackgroundTimer" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlyZe.BackgroundTimer --version 1.1.0
#r "nuget: BlyZe.BackgroundTimer, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install BlyZe.BackgroundTimer as a Cake Addin
#addin nuget:?package=BlyZe.BackgroundTimer&version=1.1.0

// Install BlyZe.BackgroundTimer as a Cake Tool
#tool nuget:?package=BlyZe.BackgroundTimer&version=1.1.0

BackgroundTimer

An easy to use timer that runs in the background and provides useful functions like Callbacks and States.

How to use

Initialize a new BackgroundTimer instance.

var timer = new BackgroundTimer();

Start the timer how you like to use it

timer.Start(TimeSpan.FromMilliseconds(69), () => Console.WriteLine("Running")); //Executes this action every 69 milliseconds

timer.Start(TimeSpan.FromMilliseconds(69), new Action[] //Executes this actions in a row every 69 Milliseconds
{
    () => Console.WriteLine("Action 1"),
    () => Console.WriteLine("Action 2"),
    () => Console.WriteLine("Action 3")
});

timer.Start(TimeSpan.FromMilliseconds(69), new Action[] //Executes this actions parallel every 69 Milliseconds - it is random which Action is executed first, second, usw.
{
    () => Console.WriteLine("Action 1"),
    () => Console.WriteLine("Action 2"),
    () => Console.WriteLine("Action 3")
}, true);

You can create a callback method

static void BackgroundTimerCallback(int tick) => Console.WriteLine("Current tick: " + tick); //tick is the current tick of the running timer

Then you can start the timer with that callback method so it get executed on every tick

timer.Start(TimeSpan.FromMilliseconds(69), BackgroundTimerCallback); //Starts a timer that executes the callback method every 69 milliseconds

To stop the timer just call the Stop(); or StopAsync(); method

timer.Stop(); //Stops the timer

await timer.StopAsync(); //Stops the timer asynchronously

await timer.StopAsync(TimeSpan.FromSeconds(5)); //Stops the timer asynchronously after 5 seconds

You can get information about a timer instance

int currentTick = timer.CurrentTick; //Get the current tick the timer is on
BackgroundTimerState currentState = timer.State; //Get the current timer state
TimeSpan timerPeriod = timer.Period; //Get the period the timer is running on

Important Note

You can only run one timer with one instance. If you want to run multiple timers simultaneously you have to create multiple timer instances.

Wrong Example

var timer = new BackgroundTimer();

timer.Start(TimeSpan.FromMilliseconds(69), BackgroundTimerCallback, TimeSpan.FromSeconds(1.5));
timer.Start(TimeSpan.FromMilliseconds(420), BackgroundTimerCallback); //That timer will not run

Right Example

var timer = new BackgroundTimer();

timer.Start(TimeSpan.FromMilliseconds(69), BackgroundTimerCallback, TimeSpan.FromSeconds(1.5)); //Starts the timer

await Task.Delay(2500); //Imitates timer running

//NOTE: You have to use the StopAsync(); method or a while loop to wait for the timer to stop otherwise the timer.Start(); executes before the timer actually stopped!

//First variant - async
if (timer.State is not BackgroundTimerState.NotRunning) await timer.StopAsync();

//Second variant - loop
if (timer.State is not BackgroundTimerState.NotRunning) timer.Stop();
while (timer.State is not BackgroundTimerState.NotRunning) { }

timer.Start(TimeSpan.FromMilliseconds(420), BackgroundTimerCallback); //Start new timer safely
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 36 9/17/2022
1.0.0 66 7/28/2022

Added the opportunity to start a new timer without needing a new instance