CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
AsyncManualResetEvent.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AsyncManualResetEvent.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Async/await implementation of a ManualResetEvent</summary>
7//-----------------------------------------------------------------------
8
9// Code from Stephen Toub @ Microsoft
10// https://blogs.msdn.microsoft.com/pfxteam/2012/02/11/building-async-coordination-primitives-part-1-asyncmanualresetevent/
11using System.Threading;
12using System.Threading.Tasks;
13
15{
20 {
21 private volatile TaskCompletionSource<bool> _tcs = new TaskCompletionSource<bool>();
22
26 public Task WaitAsync() { return _tcs.Task; }
27
31 public void Set() { _tcs.TrySetResult(true); }
32
36 public void Reset()
37 {
38 while (true)
39 {
40 var tcs = _tcs;
41 if (!tcs.Task.IsCompleted ||
42 Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource<bool>(), tcs) == tcs)
43 return;
44 }
45 }
46 }
47}
Async/await implementation of a ManualResetEvent
void Reset()
Reset the event, preparing it for reuse
Task WaitAsync()
Get awaitable task for the event
void Set()
Set the event, unblocking any code awaiting the event