![]() |
VOOZH | about |
dotnet add package NeoSmart.AsyncLock --version 3.2.1
NuGet\Install-Package NeoSmart.AsyncLock -Version 3.2.1
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageVersion Include="NeoSmart.AsyncLock" Version="3.2.1" />Directory.Packages.props
<PackageReference Include="NeoSmart.AsyncLock" />Project file
paket add NeoSmart.AsyncLock --version 3.2.1
#r "nuget: NeoSmart.AsyncLock, 3.2.1"
#:package NeoSmart.AsyncLock@3.2.1
#addin nuget:?package=NeoSmart.AsyncLock&version=3.2.1Install as a Cake Addin
#tool nuget:?package=NeoSmart.AsyncLock&version=3.2.1Install as a Cake Tool
AsyncLock is an async/await-friendly lock implementation for .NET Standard, making writing code like the snippet below (mostly) possible:
lock (_lockObject)
{
await DoSomething();
}
Unlike most other so-called "async locks" for C#, AsyncLock is actually designed to support the programming paradigm lock encourages, not just the technical elements. You can read more about the pitfalls with other so-called asynchronous locks and the difficulties of creating a reentrance-safe implementation here.
With AsyncLock, you don't have to worry about which thread is running what code in order to determine whether or not your locks will have any effect or if they'll be bypassed completely, you just write code the way you normally would and you'll find AsyncLock to correctly marshal access to protected code segments.
There are only three functions to familiarize yourself with: the AsyncLock() constructor and the two locking variants Lock()/LockAsync() .
AsyncLock() creates a new asynchronous lock. A separate AsyncLock should be used for each "critical operation" you will be performing. (Or you can use a global lock just like some people still insist on using global mutexes and semaphores. We won't judge too harshly.)
Everywhere you would normally use lock (_lockObject) you will now use one of
using (_lock.Lock()) orusing (await _lock.LockAsync())That's all there is to it!
Much like theSemaphoreSlim class, AsyncLock offers two different "wait" options, a blocking Lock() call and the asynchronous LockAsync() call. The utmost scare should be taken to never call LockAsync() without an await before it, for obvious reasons.
Upon using LockAsync(), AsyncLock will attempt to obtain exclusive access to the lock. Should that not be possible in the current state, it will cede its execution slot and return to the caller, allowing the system to marshal resources efficiently as needed without blocking until the lock becomes available. Once the lock is available, the AsyncLock() call will resume, transferring execution to the protected section of the code.
private class AsyncLockTest
{
var _lock = new AsyncLock();
void Test()
{
// The code below will be run immediately (likely in a new thread)
Task.Run(async () =>
{
// A first call to LockAsync() will obtain the lock without blocking
using (await _lock.LockAsync())
{
// A second call to LockAsync() will be recognized as being
// reentrant and permitted to go through without blocking.
using (await _lock.LockAsync())
{
// We now exclusively hold the lock for 1 minute
await Task.Delay(TimeSpan.FromMinutes(1));
}
}
}).Wait(TimeSpan.FromSeconds(30));
// This call to obtain the lock is made synchronously from the main thread.
// It will, however, block until the asynchronous code which obtained the lock
// above finishes.
using (_lock.Lock())
{
// Now we have obtained exclusive access.
// <Safely perform non-thread-safe operation safely here>
}
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 is compatible. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp1.0 netcoreapp1.0 was computed. netcoreapp1.1 netcoreapp1.1 was computed. netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard1.3 netstandard1.3 is compatible. netstandard1.4 netstandard1.4 was computed. netstandard1.5 netstandard1.5 was computed. netstandard1.6 netstandard1.6 was computed. netstandard2.0 netstandard2.0 was computed. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net46 net46 was computed. net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen30 tizen30 was computed. tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Universal Windows Platform | uap uap was computed. uap10.0 uap10.0 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on NeoSmart.AsyncLock:
| Package | Downloads |
|---|---|
|
ZayniFramework.Common
Common used C# .NET libraries such as configuration management, extensions API, dynamic object libraries API, reflection libraries, AOP interceptors, delegate invokers, TaskQueue, AsyncWorker... etc. It also provides common used IResult interface, dynamic BaseResult class and built-in ConsoleCommandService with ICommand interface. GitLab Repository: https://gitlab.com/ponylin1985/zayniframework |
|
|
Mindbox.EasyCaching.InMemory
A simple in-memory caching provider. |
|
|
Mindbox.EasyCaching.Redis
A simple distributed caching provider based on StackExchange.Redis. |
|
|
NGitLab.Mock
GitLab REST API .NET Client Mock Library |
|
|
NeoSmart.Synchronization
Assorted async-friendly mutexes and locks. Includes a scoped mutex (ScopedMutex) for cross-platform key-based locking, async countdown event (CountdownEvent), and others. |
Showing the top 5 popular GitHub repositories that depend on NeoSmart.AsyncLock:
| Repository | Stars |
|---|---|
|
BartoszCichecki/LenovoLegionToolkit
Lightweight Lenovo Vantage and Hotkeys replacement for Lenovo Legion laptops.
|
|
|
LenovoLegionToolkit-Team/LenovoLegionToolkit
Lenovo Legion Toolkit (LLT) is a Windows desktop utility created for Lenovo gaming laptops that replaces Lenovo Vantage, Legion Zone, and Legion Space.
|
|
|
MattParkerDev/sharpdbg
SharpDbg is a .NET managed code debugger supporting the Debug Adapter Protocol, implemented completely in C#/.NET
|
|
|
ubisoft/NGitLab
.NET client for the GitLab REST API
|
|
|
FuseCP/FuseCP
Multi Server Control Panel for Windows based on C#
|
| Version | Downloads | Last Updated |
|---|---|---|
| 3.3.0-preview1 | 18,871 | 2/23/2025 |
| 3.2.1 | 4,276,692 | 1/10/2022 |
| 3.2.0 | 26,279 | 12/15/2021 |
| 3.2.0-preview2 | 1,368 | 10/29/2021 |
| 3.2.0-preview1 | 1,004 | 10/18/2021 |
| 3.1.0 | 183,976 | 4/29/2021 |
| 3.1.0-BETA1 | 1,464 | 2/17/2021 |
| 3.0.1 | 39,811 | 12/18/2020 |
| 3.0.0 | 1,494 | 12/15/2020 |
| 0.3.0.2 | 683,167 | 5/17/2017 |
| 0.3.0.1 | 1,959 | 5/17/2017 |
| 0.3.0 | 2,071 | 5/17/2017 |
| 0.2.0 | 1,929 | 5/16/2017 |
| 0.1.0 | 2,458 | 5/14/2017 |
3.2: New TryLock() and TryLockAsync() methods, CancellationToken support for synchronous locking routines.
3.0: Smarter method of detecting recursion for faster and more-reliable locking on all platforms.
3.1: Added synchronous locking that may be intermixed with async locking.
3.2: Added TryLock() and TryLockAsync() methods.