Why Async Method always blocking thread?

后端 未结 4 1485
星月不相逢
星月不相逢 2021-01-29 02:38

I have a method async like this:

public static async Task SendMailAsync(){
...something
}

This method is very long time to return r

4条回答
  •  萌比男神i
    2021-01-29 03:09

    SendMailAsync will run synchronously until first await block inside.

    Marking method as "async" doesn't actually make it magically to run asynchronously. You have to use await inside that method

    Your code is synchronous, to run it in another thread without changing signature or code in your SendMailAsync you can use. But ideally you should rewrite SendMailAsync to use async API of MailSender

    var resultTask = Task.Run(async () => await SendMailAsync());
    OtherMethod1();
    OtherMethod2();
    var result = await resultTask;
    

提交回复
热议问题