-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncSemaphore.cs
51 lines (31 loc) · 895 Bytes
/
AsyncSemaphore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace BodDetect
{
sealed class AsyncSemaphore : IDisposable
{
private class SemaphoreRelaese : IDisposable
{
private SemaphoreSlim _semaphore;
public SemaphoreRelaese(SemaphoreSlim semaphore) => _semaphore = semaphore;
public void Dispose()
{
_semaphore.Release();
}
}
private SemaphoreSlim _semaphore;
public AsyncSemaphore() => _semaphore = new SemaphoreSlim(1);
public async Task<IDisposable> WaitAsync()
{
await _semaphore.WaitAsync();
return new SemaphoreRelaese(_semaphore);
}
public void Dispose()
{
_semaphore.Dispose();
}
}
}