Locky 1.0.0-beta

This is a prerelease version of Locky.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Locky --version 1.0.0-beta                
NuGet\Install-Package Locky -Version 1.0.0-beta                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Locky" Version="1.0.0-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Locky --version 1.0.0-beta                
#r "nuget: Locky, 1.0.0-beta"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Locky as a Cake Addin
#addin nuget:?package=Locky&version=1.0.0-beta&prerelease

// Install Locky as a Cake Tool
#tool nuget:?package=Locky&version=1.0.0-beta&prerelease                

Locky

All questions in your team about how to properly use locks can be answered with "Use Locky" from now on. It is very easy to use, because you can lock on strings. There is no risk of forgetting to assign something to a static field, because Locky is static itself (or use Lockally). Best of all: you can even use it in serious applications!

Note: if you want to use Locky in a package, then please use Lockally (also included in this package) to avoid clashing with the consumer of your package.

What is Locky's interface?

public static class Locky
{
    public static bool TryLock(string s);
    public static void Lock(string s); // optional: cancellationToken
    public static Task LockAsync(string s); // optional: cancellationToken
    public static void Release(string s);
}

How to use Locky?

In Visual Studio's Solution Explorer, right-click a project and click Manage NuGet Packages.... Browse and install "Locky".

Add

using LockyAndLockally;
Using the Lock method
Locky.Lock("ProcessA");
try
{
    // do some important work...
}
finally
{
    Locky.Release("ProcessA");
}
Using the TryLock method

if (!Locky.TryLock("ProcessB"))
{
    // import work is already going on, so let's skip it this time.
    return;
}
try
{
    // do some important work...
}
finally
{
    Locky.Release("ProcessB");
}
Using the LockAsync method
await Locky.LockAsync("ProcessC");
try
{
    // do some important work...
}
finally
{
    Locky.Release("ProcessC");
}

Ok, what's Lockally?

Ehwm... its interface won't surprise either, it's quite simple:

public class Lockally
{
    public bool TryLock(string s);
    public void Lock(string s);
    public Task LockAsync(string s);
    public void Release(string s);
}

If you're creating a library that unkown users will depend on, you probably don't want to tell them "hey, I've used "MySuperLock" as a lock, so you can't use it". If you create a library for your team or for others, then use Lockally like this:


public class SomeClass
{
    private static Lockally _lockally = new();

    public void SomeMethod()
    {
        if (!lockally.TryLock("ProcessA"))
        {
            // import work is already going on, so let's skip it this time.
            return; 
        }
        try
        {
            // do some important work...
        }
        finally
        {
            Locky.Release("ProcessA");
        }
    }
}

or make a singleton available within only your library:

internal static class MyLocky
{
    public static Lockally InMyLibrary { get; } = new();
}

and use it like this:

MyLocky.InMyLibrary.Lock("Hello world!");

Tip

In Visual Studio, place your cursor on the Release method and use Shift F12 to find out what lock names have been used by your colleagues.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 317 2/17/2023
1.1.0 233 2/17/2023
1.0.1 254 2/12/2023
1.0.0 260 2/11/2023
1.0.0-beta 149 2/11/2023