C# How to start an async method without await its complete?

前端 未结 6 1445
醉酒成梦
醉酒成梦 2021-02-05 08:12

Sometimes I need to start an async job which works very slow. I don\'t care if that job success and I need to continue working on my current thread.

Like sometimes I nee

6条回答
  •  悲哀的现实
    2021-02-05 08:43

    As Amadan told in the comment that, you need to remove async from your function. then it will stop giving you the warning.

    // This method has to be async
    public Response SomeHTTPAction()
    {
         // Some logic...
         // ...
         // Send an Email but don't care if it successfully sent.
         Task.Factory.StartNew(() =>  _emailService.SendEmailAsync());
         return MyRespond();
    }
    

    and Task.Factory.StartNew(() => _emailService.SendEmailAsync()); will indeed work on a new thread.

提交回复
热议问题