Why Async Method always blocking thread?

后端 未结 4 1481
星月不相逢
星月不相逢 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条回答
  •  灰色年华
    2021-01-29 03:14

    There are several background jobs processing library out there. I have used hangfire for some of my projects.

    But if you want to make this simple, you can use something built-in inside asp.net.

    HostingEnvironment.QueueBackgroundWorkItem((ct) =>
    {
         SendMailAsync();
    });
    
    OtherMethod1();
    OtherMethod2();
    

    Make sure you add reference to System.Web.Hosting

    You do not need SendMailAsync() to return a Task since you will not be awaiting on something. Just let the sending of email code run in background.

提交回复
热议问题