0

I tried to read Cleary's post and also googled around StackOverflow, but still doesn't get some behavior regarding async await vs await Task.Run()

My Code:

private async void ValidateLexisNexis()
    {
        _view.LogProcess(@"I'm Starting..", true);
        await Task.Run(() => SomeAsynchMethod());
        _view.LogProcess(@"Im out of SomeSynchMethod", true);
    }
private async void SomeAsynchMethod()
    {
        await Task.Delay(2000);
        MessageBox.Show(@"Hi I'm in");
    }

Now I expected:

  1. Log I'm Starting...
  2. Create a new thread and run SomeAsynchMethod(), wait for the response, Message "Hi I'm In"
  3. Log I'm out of someAsynchMethod

Now, no 2 is my problem, the code continue to no 3 before finishing no 2, and I wonder, what's the purpose of the await before Task.Run ?? shouldn't the ValidateLexisNexis wait for the response if await Task.Run before continue to log I'm out??

P.S. - I know I can just use await SomeAsynchMehtod() without Task.Run and then it works as expected, but I'm trying to understand why with Task.Run the await doesn't really wait.

Thanks!!!!

Roni Axelrad
  • 399
  • 3
  • 13

0 Answers0