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. - 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.
Available via NuGet.
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);
}
In Visual Studio's Solution Explorer
, right-click a project and click Manage NuGet Packages...
. Browse and install "Locky".
Add
using LockyAndLockally;
Locky.Lock("Process A");
try
{
// do some important work...
}
finally
{
Locky.Release("Process A");
}
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");
}
await Locky.LockAsync("Process C");
try
{
// do some important work...
}
finally
{
Locky.Release("Process C");
}
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!");
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.