wpf - Why does async routine not return control to the end user -
working in wpf mvvm pattern. have area in application runs query against underlying database returned in form of xml (albeit unformatted @ stage). aim display xml in syntax editor (once has been correctly formatted) easier end user read. depending upon date range parameters supplied end user query xml returned can run few tens of lines potentially 2 or 3 thousand lines can take time format. typical returned query of 350 lines takes 6 seconds format.
because of potential delay thought try use async routine end user did not think application had frozen. accept knowledge of async patchy , i've read available , followed couple of walkthroughs try , @ least make strat in right direction.
the routine have below works (in far query run , have formatted xml appear in syntax editor). fails in far main intention control should handed ui not appear happen , string messages i'm passing status bar (with exception of last 1 'submission document completed') aren't being shown either.
can point me in right direction i'm doing incorrectly here.
thanks
private async sub createnewersbuyerssubmissionbydate(byval obj object) siauto.main.logmessage("button clicked on ribbon") applicationstatuslabeltext = "creating submission document" formattedstring = await formatdocument() dim language new xmlsyntaxlanguage() submissionseditor.document.language = language if not string.isnullorempty(formattedstring) submissionseditor.document.settext(formattedstring) allowerssubmissionvalidation = true else allowerssubmissionvalidation = false end if applicationstatuslabeltext = "submission document completed" siauto.main.logmessage("routine finished") end sub public async function formatdocument() task(of string) siauto.main.logmessage("started format document") dim xmlvalue string = await setupnewerssubmissionbydaterange() dim language new xmlsyntaxlanguage() dim doc new codedocument {.language = language} doc.settext(xmlheader & environment.newline & xmlvalue) dim textformatter itextformatter = doc.language.getservice(of itextformatter)() if not (textformatter nothing) applicationstatuslabeltext = "formatting submission document display, please wait..." textformatter.format(doc.currentsnapshot.snapshotrange) end if siauto.main.logmessage("finished formating document") return doc.currentsnapshot.text end function
async
not mean "execute on background thread". if need push cpu-bound work background thread, use task.run
:
formattedstring = await task.run(function() formatdocument())
note formatdocument
run on background thread, can no longer access ui elements (i.e., applicationstatuslabeltext
).
Comments
Post a Comment