Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Calling async Function Inside lock Block

0.00/5 (No votes)
22 May 2016 1  
Calling async function inside lock block

Why

Sometimes, you just want to execute an async call inside a lock statement.

By default, you cannot accomplish this functionality, the compiler will get really mad at you because you are trying to create context-switching inside a lock.

I'm not saying that this is the cleanest software design that I could think of, but if you want – you can!

How

Here is how you can work around this issue:

In the below functions, you can notice that they use ManualResetEvent, it gives them the ability to wait inside the lock statement although the context-switching. We are simply waiting for the event.

*If your Func throws exception, you will not get in outer try-catch, you need to be aware of it.

Code

public static Task Execute(object lockObject, Func<Task> func)
{
    return Task.Run(() =>
    {
        ManualResetEvent e = new ManualResetEvent(false);
        lock (lockObject)
        {
            bool wait = true;

            func().ContinueWith((taskResult) =>
            {
                wait = false;
                e.Set();
            });

            if (wait)
            {
                e.WaitOne();
            }
        }
    });
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here