c# async task with no await statement

前端 未结 1 795
谎友^
谎友^ 2020-12-21 23:20

I\'m just starting with c# development, and i always strugled with asyncronous programming,Ii have a doubt about performing a task asyncrounously.

I have my own serv

相关标签:
1条回答
  • 2020-12-21 23:54

    Regarding what you read in the docs, think about this this way:

    public async Task OuterMethod()
    {
        InnerMethod();
    }
    

    You've marked OuterMethod async, yet you're not doing any awaiting. The docs are simply saying that OuterMethod will run synchronously, and you may as well just mark it with void instead of async Task. However, the fact that OuterMethod runs synchronously doesn't change the nature of InnerMethod; it may well be asynchronous, and if you're not awaiting it, it will run fire-and-forget style, most likely on a background thread.

    In your specific case, InnerMethod would be the method that sends the email. If you're certain you don't care whether sending the email succeeds or fails, I'd suggest calling SmtpClient.SendAsync without awaiting.

    0 讨论(0)
提交回复
热议问题