Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Tip/Trick

Calling async Function Inside lock Block

Rate me:
Please Sign up or sign in to vote.
3.80/5 (4 votes)
22 May 2016CPOL 24K   6   12
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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerSemaphoreSlim.WaitAsync Pin
JimBob SquarePants24-May-16 18:13
JimBob SquarePants24-May-16 18:13 
GeneralRe: SemaphoreSlim.WaitAsync Pin
Pavel Durov26-May-16 23:01
professionalPavel Durov26-May-16 23:01 
Thumbs Up | :thumbsup:
SuggestionNot a parallel work Pin
InbarBarkai19-May-16 20:18
InbarBarkai19-May-16 20:18 
GeneralRe: Not a parallel work Pin
Pavel Durov19-May-16 22:23
professionalPavel Durov19-May-16 22:23 
GeneralRe: Not a parallel work Pin
InbarBarkai21-May-16 23:49
InbarBarkai21-May-16 23:49 
GeneralRe: Not a parallel work Pin
Pavel Durov22-May-16 0:07
professionalPavel Durov22-May-16 0:07 
GeneralRe: Not a parallel work Pin
InbarBarkai22-May-16 20:56
InbarBarkai22-May-16 20:56 
GeneralRe: Not a parallel work Pin
Pavel Durov22-May-16 21:12
professionalPavel Durov22-May-16 21:12 
GeneralRe: Not a parallel work Pin
InbarBarkai24-May-16 18:39
InbarBarkai24-May-16 18:39 
GeneralRe: Not a parallel work Pin
Pavel Durov26-May-16 21:13
professionalPavel Durov26-May-16 21:13 
BugJust mistyped: async, not asnyc Pin
Dmitriy Gakh19-May-16 5:23
professionalDmitriy Gakh19-May-16 5:23 
GeneralRe: Just mistyped: async, not asnyc Pin
Pavel Durov19-May-16 21:17
professionalPavel Durov19-May-16 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.