-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathAsyncAutoResetEvent.cs
55 lines (46 loc) · 1.01 KB
/
AsyncAutoResetEvent.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
52
53
54
55
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Uno.Extensions;
public class AsyncAutoResetEvent : IDisposable
{
private readonly AutoResetEvent _event;
private bool isDisposed;
public AsyncAutoResetEvent(bool initialState)
{
_event = new AutoResetEvent(initialState);
}
public Task<bool> Wait(TimeSpan? timeout = null)
{
return Task.Run(() =>
{
if (timeout.HasValue)
{
return _event.WaitOne(timeout.Value);
}
return _event.WaitOne();
});
}
public void Set()
{
_event.Set();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
if (disposing)
{
// free managed resources
_event?.Dispose();
}
isDisposed = true;
}
}