Yort.DeadManSwitch

A reusable implementation of a dead man switch. A dead man switch 'activates' (causes something to happen) when some event hasn't occurred for a specified period of time. It is effectively a resetable timer.

View on GitHub

Yort.DeadManSwitch

What is it?

A reusable implementation of a dead man switch. A dead man switch ‘activates’ (causes something to happen) when some event hasn’t occurred for a specified period of time. It is effectively a resetable timer.

When would I use it?

Any time you want to take some action because something else hasn’t happened for a while, this includes but is not limited to;

GitHub license

Build status

Supported Platforms

Currently;

Available on Nuget

    PM> Install-Package Yort.DeadManSwitch

NuGet Badge

How do I use it?

Create a switch passing the action to call when the switch activates, the delay before activation, and other settings to the constructor. Call the Reset method of the switch each time a regular event occurs. When Reset hasn’t been called within the delay period specified, the switch will activate.

See the demo console app in the repo for sample usage

    using Yort.DeadManSwitch;

    //This switch activates after 5 seconds on inactivity, and automatically
    //resets itself after activation.
    var dms = new DeadManSwitch(5000, () => Console.WriteLine("Switch activated!")), (reason) => Console.WriteLine("Reset because " + reason.ToString()), true);

    //Somewhere else in the code, in a code path that should execute regularly within 5 seconds
    dms.Reset();

    //To stop the switch, dispose it.
    dms.Dispose();