c# - Async method in an async method -
my question: how wait input async method before going on?
some background info: have c# app scans qr code, , loads data on basis of value scanned. above works, want app ask whether value scanned right card.
the applied code follows:
using zxing.mobile; mobilebarcodescanner scanner; private bool correct = false; //create new instance of our scanner scanner = new mobilebarcodescanner(this.dispatcher); scanner.dispatcher = this.dispatcher; await scanner.scan().continuewith(t => { if (t.result != null) handlescanresult(t.result); }); if (continue) { continue = false; frame.navigate(typeof(characterview)); }
handlescanresult(result)
being (skimmed down):
async void handlescanresult(zxing.result result) { int idscan = -1; if (int.tryparse(result.text, out idscan) && idscan != -1) { string confirmtext = carddata.names[idscan] + " found, card wanted?"; messagedialog confirmmessage = new messagedialog(confirmtext); confirmmessage.commands.add(new uicommand("yes") { id = 0 }); confirmmessage.commands.add(new uicommand("no") { id = 1 }); iuicommand action = await confirmmessage.showasync(); if ((int) action.id == 0) continue = true; else continue = false; } }
the problem is, continue
remains false the moment if (continue)
called in 1st block of code, due message box being asynchronous , app continuing if-statement before message box done.
i have tried giving handlescanresult()
task return type , calling await handlescanresult(t.result);
. should make app wait handlescanresult()
before going on if-statement. however, returns following error:
the 'await' operator can used within async lambda expression. consider marking lambda expression 'async' modifier.
hence question on how wait input before going on.
you cannot wait until async void
method has finished. task
return type allows callers sort of signal indicating operation has completed. did right thing changing method async task
.
as error message, it's telling you can rid of compiler error follows:
await scanner.scan().continuewith(async t => { if (t.result != null) await handlescanresult(t.result); });
note async
before t
.
however, because compiles, doesn't mean should doing. you're making things overly complicated, , don't need use continuewith
@ here. when you're in async
method body, await
operator you're using can continuewith
would've done, in more straightforward manner.
var scanresult = await scanner.scan(); if (scanresult != null) await handlescanresult(scanresult);
Comments
Post a Comment