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.
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.
Currently;
- .Net Standard 1.2
- .Net Standard 2.0
- .Net 4.0+
PM> Install-Package Yort.DeadManSwitch
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();