C
C#3y ago
nahr

❔ 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
nahr
nahrOP3y ago
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
mtreit
mtreit3y ago
CPU bound code should not be async
nahr
nahrOP3y ago
"For CPU-bound code, you await an operation that is started on a background thread with the Task.Run method" Task.Run method, aha
mtreit
mtreit3y ago
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
MODiX
MODiX3y ago
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)
From Scott Hanselman (@shanselman)
Twitter | React with ❌ to remove this embed.
nahr
nahrOP3y ago
oh nice, see you at sunday then
Tvde1
Tvde13y ago
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
var input1 = CalculateALotOfStuff();
var input2 = CalculateMoreStuff();
var input3 = DoSomeMoreCalculation();

SaveResult(input1, input2, input3);
var input1 = CalculateALotOfStuff();
var input2 = CalculateMoreStuff();
var input3 = DoSomeMoreCalculation();

SaveResult(input1, input2, input3);
This could benefit from running three calculations in parallel
var input1 = CalculateALotOfStuff();
var input2 = CalculateMoreStuff(input1);
var input3 = DoSomeMoreCalculation(input2);

SaveResult(input3);
var input1 = CalculateALotOfStuff();
var input2 = CalculateMoreStuff(input1);
var input3 = DoSomeMoreCalculation(input2);

SaveResult(input3);
But this wouldn't, it would only add overhead if you move code through different tasks that are ran in sequence
mrphil2105
mrphil21053y ago
Well that depends If your CPU-bound code takes a long time to finish using Task.Run might exhaust the thread pool
Accord
Accord3y ago
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.
Want results from more Discord servers?
Add your server