A naive implementation of async lock

Behind the scenes the implementation uses SemaphoreSlim to do all the heavy lifting which already has a support for async/await.

You would normally use AsyncLock within a “using” statement. To keep the implementation as simple as possible I do not use any cancellation tokens for any of the operations.


private readonly AsyncLock _lock = new AsyncLock();
public async Task DoSomethingAsync()
  using (await _lock.EnterAsync())
    await Task.Delay(TimeSpan.FromSeconds(1));


Retrieving state in a FirstChanceException in .NET — Network Programming in .NET

If you’re using a application-wide FirstChanceException handler, this is great for covering lots of bases at once, it helps make sure that you can catch (and record) an exception even if it happens with a bit of code you didn’t expect. However, the scope of the handler is what it is, so accessing state, and […]

Retrieving state in a FirstChanceException in .NET — Network Programming in .NET

C#: Thread Safe Event calls

Sudhanshu's Ode to Code

Hello World,

While working on a multi-threaded Windows service that used events heavily, I did some research on how to trigger events in a thread safe manner. Inevitably, one looks for reference material or books that have the respect of the community and Jeffery Richter’s CLR via C# is one such resource.

The Context

In the chapter on Events (Chapter 11, pg 265 in the eBook), Jeffery summarizes the first 3 steps of how to raise events in a thread safe manner by providing a snippet for an extension method that can do this as such:

The above code is supposed to be an improvement over the following way where one uses a temp variable to store the passed in delegate instance and then compares the temp to null:

The reason why he proposes the code in the extension method over the simpler approach above is that though in theory…

View original post 262 more words

Blog at

Up ↑