Skip to content

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.

License

Yortw/Yort.DeadManSwitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;

  • Logging or sending an alert when you haven't received a (network or other) message for X minutes/hours.
  • Performing a search x millseconds after the last key pressed in an auto-search/complete text field.
  • Starting archive/optimisation processes when there's been no user activity for a while.

GitHub license

Build status

Supported Platforms

Currently;

  • .Net Standard 1.2
  • .Net Standard 2.0
  • .Net 4.0+

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();

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published