Blog: 2015 November

Inside Async/Await: Synchronize an async method or code block

November 5, 2015, (updated on November 10, 2015), Software Development

Have you ever tried to await a task inside a lock() block? It is not possible, because you can only synchronize synchronous code with the lock keyword. However, in today’s .NET development, async/await is used everywhere and the need to synchronize asynchronous code blocks is coming up quite often. This is why I wrote a simple class which can be used to synchronize asynchronous code blocks. As an example for this article, we have a simple synchronous method which is synchronized using a lock() block: private object _lock = new object(); public void Run(int i) { lock (_lock) { Console.WriteLine("Before: […] Read more...

Tags: , , , , ,

  Page 1 of 1