I need to do a fire and forget call to some async method. I realised VS is suggesting that I can set the call to a _discard and the IDE warning goes away. But I'm not sure if that call is still not awaited when used with the discard. Would it be?
public async Task<object> SomeSideTaskToBeForgotten()
{
...blah blah
}
public async Task MainTask()
{
..some stuff
_ = SomeSideTaskToBeForgotten(); //is this still fire and forget?
..some other stuff
}
Yes, it's still fire and forget.
When SomeSideTaskToBeForgotten();
returns a Task
, the remainder of your method will execute, without waiting for the Task
to complete.
The discard just makes explicit the fact that the Task
isn't required for any further processing.
VS will be recommending the discard because SomeSideTaskToBeForgotten();
returns something i.e. not void.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With