c# - await method in task -
i work on document downloader in windows store app, , have problem tasks. here sample of code :
task created , started
... httpdownloader httpdownloader = new httpdownloader(server); action<object> action = (object doc ) => httpdownloader.downloaddocument((document)doc); task t1 = new task(action,selection.doc); t1.start(); ... downloaddocument method
... filesavepicker savepicker = new filesavepicker(); savepicker.suggestedstartlocation = pickerlocationid.documentslibrary; // dropdown of file types user can save file savepicker.filetypechoices.add("application/pdf", new list<string>() { ".pdf" }); // default file name if user not type 1 in or select file replace savepicker.suggestedfilename = doc.name+"_"+doc.version; storagefile file = await savepicker.picksavefileasync(); // here exception launch. ... and every time :
element not found (exception de hresult : 0x80070490)
without task, code works fine, since want use tasks manage different download, have error.
your action runs on random pool thread, different main thread (as scheduled task.start). there access doc object which, assume, created on main thread. might reason fault.
generally, should not access objects (especially ui elements) across differed threads, unless designed thread-safe.
edited: not need task here. await savepicker.picksavefileasync() , mark outer method async (the method creates task).
to understand better thread you're on, may add debug trace this:
debug.print("<method name>, thread: {0}", thread.currentthread.managedthreadid);
Comments
Post a Comment