c# - Wait on function to complete -
in 1 of functions i'm getting document:
controller.retrievedocument(documentid); // here want work after document has finished loading
this how function build up:
public async void retrievedocument(string documentid){ documentresult documentresult = await getdocumenttask (documentid); // checks here }
the called function this:
private async task<documentresult> getdocumenttask (string documentid){ try{ loadingspinner.show(); documentresult = await task.run(() => manager.getdocument (documentid)); loadingspinner.hide(); } catch(exception ex) { // } }
now want if retrievedocument
has been finished other work. problem retrievedocument
asynchronous , other code executed before function has finished loading. there 2 options comes mind:
- wait on
retrievedocument
somehow. - make synchronous.
- use separate event in
retrievedocument
i don't know how should wait on retrievedocument
described in 1. tried make synchronous loading spinner not correctly show. seems waits on webservice call , if has been finished spinner shown shortly. introduces kind of lag. if use asynchronously don't behavior , loading spinner correctly shown. no. 3 option seems best, how other options work?
what i'm missing here?
you either want rewrite function as...
public async task retrievedocument(string documentid){ documentresult documentresult = await getdocumenttask (documentid); // checks here }
or
public task retrievedocument(string documentid){ task<documentresult> documentresult = getdocumenttask (documentid); // checks here return documentresult; }
however want former.
you need return task
, object wraps concepts of, work in flight, completion event. async
await
clever syntaxic sugar, few cool libraries added in (mostly error handling).
Comments
Post a Comment