Posts

Showing posts from August, 2015

How to lock a block containing await operation in C#

Save below code in a file say AsyncLock.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace namespaceName {     // http://blogs.msdn.com/b/pfxteam/archive/2012/02/12/10266983.aspx     public class AsyncSemaphore     {         private readonly static Task s_completed = Task.FromResult(true);         private readonly Queue<TaskCompletionSource<bool>> m_waiters =new Queue<TaskCompletionSource<bool>>();         private int m_currentCount;         public AsyncSemaphore(int initialCount)         {             if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");             m_currentCount = initialCount;         }         public Task WaitAsync()         {             lock (m_waiters)             {                 if (m_currentCount > 0)                 {                     --m_currentCount;