What's the Semantic difference between those two methods?
public Task DoSomething()
{
return Task.Run(() => MyFunction());
}
public async Task DoSomethingAsync()
{
await Task.Run(() => MyFunction());
}
Is there something I should think to choose between one or the other?
The actual difference is this:
Task.Run and return the resulting taskTask.Run, then queue up a continuation that when this task completes, will continue executing your methodIn terms of "which one should I choose", here's the general guideline I use:
If all your method is going to do in relation to tasks is to return them then do not use
async/await
If, instead, you need to "wait for" a task to complete, then do more stuff, then useasync/await.
I am sure there are exceptions to this but I have yet to find any.
So of those two methods, pick the first.
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