c# - Using async/await to improve performance -
i have service method runs set of "rules" on incoming data. of these rules perform simple checks validating id number, , go out databases lookup information. looks pretty this:
public void runrules(validationrequest request) { // each rule returns bool example var rules = getrules(); var results = list<bool>(); parallel.foreach(rules, rule => { var result = rule(); lock(results) results.add(result); }); foreach(var result in results) { recordresultindatabase(result); } callnextservice(somenewrequest); } i think (but not sure) improve things using async/await. idea is, when 1 rule finishes call recordresultindatabase while other rules still running.
as requirement though, wouldn't want call callnextservice happen until rules have ran , results recorded in database.
problem i'm absolute noob async/await , think (but not sure) task.waitall might i'm looking for.
i'm uncertain of how this, i'm guessing:
list<task<bool>> ruletasks = getrules(); task.waitall(ruletasks); // somehow results of rules , record them in db???
i don't think async-await improve performance in case. in short, async-await useful in 2 cases:
- to improve responsiveness of gui applications.
- to improve scalability of server applications.
neither of seems case, there no reason use async-await in code.
if want call recordresultindatabase() while requests being processed, instead of when of them done, can use plinq instead of parallel.foreach(). benefit, simplify code:
var results = getrules().asparallel().select(rule => rule()); foreach (var result in results) { recordresultindatabase(result); }
Comments
Post a Comment