Latch makes sure that a piece of code is not executed when another piece of code is executing.
This package is based on the following articles:
- http://www.minddriven.de/index.php/technology/development/design-patterns/latch-design-pattern
- http://codebetter.com/jeremymiller/2007/07/02/build-your-own-cab-12-rein-in-runaway-events-with-the-quot-latch-quot/
// Remove the event handler for the moment
someComboBox.SelectedIndexChanged -= someHandler;
// do something that would probably make someComboBox fire the SelectedIndexChanged event
// Put the event handler back
someComboBox.SelectedIndexChanged += someHandler;
This example shows a common WinForms problem which can be solved with a latch.
// Declare the Latch as field: Latch someComboBoxLatch = new Latch();
someComboBoxLatch.RunInsideLatch(() =>
{
// do something that would probably make someComboBox fire the SelectedIndexChanged event
});
Surrounding the critical code by the latch makes sure it is only executed once. Recursive calls and StackOverflows can be avoided.
// Declare the FullLatch as field: FullLatch someLatch = new FullLatch();
someLatch.LatchAndRun(() =>
{
// do something and make sure the latch is set
});
someLatch.RunIfNotLatched(() =>
{
// do something that should only run when not latched
});
This way you will have full control over the latch, and you can choose which parts latch and which parts are only run if not latched.
- Microsoft Visual Studio Community
- JetBrains Resharper
- NUnit
- FakeItEasy
- Inedo ProGet
- Icons8
- NuGet
- GitHub
version 1.0.3: Added .NET 8.0
version 1.0.2: Added .NET 5.0
version 1.0.1: Added .NET standard 2.0
version 1.0.0: First version (.NET Framework 4.5.2)