Locky 1.0.1
See the version list below for details.
dotnet add package Locky --version 1.0.1
NuGet\Install-Package Locky -Version 1.0.1
<PackageReference Include="Locky" Version="1.0.1" />
paket add Locky --version 1.0.1
#r "nuget: Locky, 1.0.1"
// Install Locky as a Cake Addin #addin nuget:?package=Locky&version=1.0.1 // Install Locky as a Cake Tool #tool nuget:?package=Locky&version=1.0.1
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
).
Notes:
- 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.<br/> - once a string has been used as a lock, it is not removed from the dictionary that is used internally (until your app restarts). Using Locky for a few thousand different strings is probably OK, but start to think about memory usage if you have variable locks such as contract Ids.
<p align="center"> <img src="https://avatars.githubusercontent.com/u/125100496?s=100&u=dfb896642d9b9e298628e8dd804202ed3c5e1386&v=4" alt="Locky logo"/> </p>
Available via NuGet.
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"))
{
// important 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"))
{
// important work is already going on, so let's skip it this time.
return;
}
try
{
// do some important work...
}
finally
{
_lockally.Release("ProcessA");
}
}
}
or make a singleton available within only your library in two steps:
Step 1: make a static container for a Lockally
instance.
using LockyAndLockally;
namespace MyLibrary;
internal static class MyLockyContainer
{
public static Lockally MyLocky { get; } = new();
}
Step 2: add the container as a static global using to your 'Usings.cs':
global using static MyLibrary.MyLockyContainer;
and use it like this anywhere in your project:
MyLocky.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 | Versions 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. |
-
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 |