1

I'm new to c# async await mechanism. I read some articles about async all the way (http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). I have an example below, could you please let me know if the first Initialize method will cause dead lock and why? Thank you in advance.

public class Test {

    public  async  Task DoSomeWorkAsync() {
      await DoSomeWork1Async(); 
    }

    // This method is mixed with Wait and async await, will this cause lead lock?
    public void Initialize() {
      Task.Run(() => DoSomeWorkAsync()).Wait();
    }

    // This method is following async all the way
    public async Task InitializeAsync()  {
      await DoSomeWorkAsync();
    }

}

// Update: Here is the context where two Initialize methods are called
public class TestForm : Form {
  // Load Form UI
  public async void OnLoad() {
    var test = new Test();
    test.Initialize();
    await test.InitializeAsync();
  }
}
6
  • 2
    It depends on context where you are using it. Commented Aug 24, 2015 at 17:57
  • In the Initialize() method, I would simply do DoSomeWorkAsync().Wait(). Using Task.Run() will start a new thread and call DoSomeWorkAsync() synchronously. Commented Aug 24, 2015 at 18:00
  • thanks guys I added context where they are being used. Commented Aug 24, 2015 at 18:02
  • @PhilippeParé: thx Philippe, but will DoSomeWorkAsync().Wait() cause dead lock? I assume async-await all the way down means once you use asyn-await, you can't mix Wait(). Please Correct me if I'm wrong. Commented Aug 24, 2015 at 18:06
  • 1
    @PhilippeParé no. It will deadlock as in it will get stuck forever. Commented Aug 24, 2015 at 18:11

1 Answer 1

6

No, this will not deadlock because you're blocking on a task that's being executed by a ThreadPool thread with no SynchronizationContext. Since it isn't running on the UI thread there's nothing stopping that task from completing and so there's no deadlock.

If this was your code, it will have deadlocked:

public void Initialize()
{
    DoSomeWorkAsync().Wait();
}

This is still not a good reason to block though, you should use async-await all they way up.

Sign up to request clarification or add additional context in comments.

5 Comments

@i3amon: thx i3amon. The reason I wanna use Initialize() over InitializeAsync() is because class TestForm has a lock object, which prevent OnLoad method being called concurrently, which looks like this: lock(lockObj) {Initialize(); } However await InitializeAsync(); could not be called within lock statement.
@macio.Jun then use an AsyncLock instead
@macio.Jun sure.. any time.
@i3amon, sorry I have one more unclear point. Is there a difference between Initialize method above and public void Initialize() {Task.Run(async() => await DoSomeWorkAsync()).Wait();} ?
@macio.Jun not really, the async and await are redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.