AsyncBlocker.NET
1.0.1.5
dotnet add package AsyncBlocker.NET --version 1.0.1.5
NuGet\Install-Package AsyncBlocker.NET -Version 1.0.1.5
<PackageReference Include="AsyncBlocker.NET" Version="1.0.1.5" />
paket add AsyncBlocker.NET --version 1.0.1.5
#r "nuget: AsyncBlocker.NET, 1.0.1.5"
// Install AsyncBlocker.NET as a Cake Addin
#addin nuget:?package=AsyncBlocker.NET&version=1.0.1.5
// Install AsyncBlocker.NET as a Cake Tool
#tool nuget:?package=AsyncBlocker.NET&version=1.0.1.5
AsyncBlocker.NET
Start
and Stop
in a thread safe way using a very simple Semaphore
Prevents the blocked Method
from executing again until Stop
or ReleaseWhenTaken
is called.
Usage
using System.Threading.Tasks
AsyncBlocker asyncBlocker = new AsyncBlocker(); // Lock
AsyncBlockerSimple asyncBlocker = new AsyncBlockerSimple(); // No Lock
Start
Calling Start
will block the current Method
from executing again until Stop
or ReleaseWhenTaken
is called.
public void Start()
{
bool taken = asyncBlocker.Start();
if (taken)
{
// Your Code
}
else
{
return;
}
}
Stop
Calling Stop
or ReleaseWhenTaken
from any thread will allow the blocked Method
to be called again
asyncBlocker.Stop();
asyncBlocker.ReleaseWhenTaken();
Test Console Example
Below is a full example of three threads fighting over one AsyncBlockerSimple
One
and Two
can not execute the Method
again until Three
calls Stop
, preventing double execution.
One
and Two
can not execute the Method
at the same time.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
internal class Program
{
static bool run = true;
static volatile int last1 = 0;
static volatile int last2 = 0;
static volatile int counter1 = 0;
static volatile int counter2 = 0;
static volatile int counter3 = 0;
static void Main()
{
AsyncBlockerSimple asyncBlocker = new AsyncBlockerSimple();
// One
Task.Run(async () =>
{
while (run)
{
bool one = asyncBlocker.Start();
if (one)
{
counter1++;
}
else
{
counter3++;
}
await Task.Delay(50);
}
});
// Two
ThreadPool.UnsafeQueueUserWorkItem(async (_) =>
{
while (run)
{
bool two = asyncBlocker.Start();
if (two)
{
counter2++;
}
else
{
counter3++;
}
await Task.Delay(50);
}
}, null);
// Three
ThreadPool.UnsafeQueueUserWorkItem(async (_) =>
{
while (run)
{
asyncBlocker.Stop();
await Task.Delay(300);
Console.SetCursorPosition(0, 0);
Console.WriteLine("One : " + counter1);
Console.WriteLine("Two : " + counter2);
Console.WriteLine("Skipped: " + counter3);
if (counter1 > last1)
{
if (counter2 != last2)
{
run = false;
}
}
if (counter2 > last2)
{
if (counter1 != last1)
{
run = false;
}
}
last1 = counter1;
last2 = counter2;
}
Console.WriteLine("Failed");
}, null);
Console.ReadLine();
}
}
}
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 |
---|
Add AsyncBlockerSimple which is similar to AsyncBlocker but it does not use a lock