Skip to content
/ Latch Public

Latch makes sure that a piece of code is not executed when another piece of code is executing.

License

Notifications You must be signed in to change notification settings

denxorz/Latch

Repository files navigation

Latch

.Build status Coverage Status NuGet License

What does it do?

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:

Examples

// 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.

Tools and Products Used

Versions & Release Notes

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)

About

Latch makes sure that a piece of code is not executed when another piece of code is executing.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages