.net - asyn example calling multiple method -
i learning async programming, not sure if going in right direction. want call multiple method async read asyn should start top level follow lowest level. @ top level when asyn method compiler complaining cannot wait string.
public static async void method() { var classdemoasyn = new demoasyn(); var t = await classdemoasyn.dosomeimportantwork("start ").result; } public class demoasyn { public async task<string> dosomeimportantwork(string strdosomework) { strdosomework = strdosomework + " (1) enter => important work"; console.writeline("**********enter => important work******"); var t = await somecomplexoperation(strdosomework); console.writeline("***********end important work***********"); return t; } public task<string> somecomplexoperation(string strdosomecomplexwork) { string str =strdosomecomplexwork + "enter => very complex work"; console.writeline(str); return task.factory.startnew(() => {return str; }); } }
the error you're getting because you're using both .result
, await
@ same time (as pointed out daniel kelley).
a common simple approach use async
in console applications have top-level async task
method , synchronously wait on in main()
method (this pretty place wait()
or result
make sense in async-using code), e.g.:
void main() { mainasync().wait(); }
another option use stephen cleary's asynccontext.run()
.
Comments
Post a Comment