❔ Async Tasks (utility)
Hi, guys.
Been doing some refactor work and been upgrading an old project from sync to async.
But I'm not really sure if it's worth to upgrade smaller utility methods to async Tasks.
Microsoft says that I/O-bound code and CPU-bound code should be async.
So I have upgraded all my actions methods in the controllers, wrapper methods, repo methods.
But for instance what about a minor utility method that is called from an async Task in my controller, should it also be async?
9 Replies
maybe this question is stupid.
I mean you should just use an async method if you can await anything in the body of the method
CPU bound code should not be async
"For CPU-bound code, you await an operation that is started on a background thread with the Task.Run method"
Task.Run method, aha
It just so happens I'm giving a talk on this topic at the Solution1 conference that this Discord community is hosting starting later this week
Scott Hanselman (@shanselman)
This is awesome. The first community discord organized @dotnet C# conference! Check it out below, it’s a week of awesome. #dotnet
Quoted by
<@!406536255426396160> from #solution1 (click here)
Twitter | React with ❌ to remove this embed.
oh nice, see you at sunday then
Making something async doesn't make it magically faster. It does however give you the opportunity to run some more code while waiting for it to complete
This could benefit from running three calculations in parallel
But this wouldn't, it would only add overhead if you move code through different tasks that are ran in sequence
Well that depends
If your CPU-bound code takes a long time to finish using Task.Run might exhaust the thread pool
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.