Locky 1.1.1
dotnet add package Locky --version 1.1.1
NuGet\Install-Package Locky -Version 1.1.1
<PackageReference Include="Locky" Version="1.1.1" />
paket add Locky --version 1.1.1
#r "nuget: Locky, 1.1.1"
// Install Locky as a Cake Addin #addin nuget:?package=Locky&version=1.1.1 // Install Locky as a Cake Tool #tool nuget:?package=Locky&version=1.1.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 via both synchronous and asynchronous methods. There is no risk of forgetting to assign something to a static field, because Locky
is static itself (or use Lockally
which is also included).
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("Process A");
try
{
// do some important work...
}
finally
{
Locky.Release("Process A");
}
Using the TryLock
method
if (!Locky.TryLock("Process B"))
{
// important work is already going on, so let's skip it this time.
return;
}
try
{
// do some important work...
}
finally
{
Locky.Release("Process B");
}
Using the LockAsync
method
await Locky.LockAsync("Process C");
try
{
// do some important work...
}
finally
{
Locky.Release("Process C");
}
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("Process D"))
{
// important work is already going on, so let's skip it this time.
return;
}
try
{
// do some important work...
}
finally
{
_lockally.Release("Process D");
}
}
}
or make a singleton available within only your library in two steps:
Step 1: make an internal 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 | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.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 | 315 | 2/17/2023 |
1.1.0 | 230 | 2/17/2023 |
1.0.1 | 252 | 2/12/2023 |
1.0.0 | 257 | 2/11/2023 |
1.0.0-beta | 147 | 2/11/2023 |
See CHANGELOG.md at GitHub.