c# - WebClient GET/POST without blocking UI -


i make non blocking , post requests. i've managed solve backgroundworker, need achieve using tasks.

public task<string> post(uri uri, string data) {     return _webclient.uploadstringtaskasync(uri, data); }  public task<string> get(uri uri) {     return _webclient.downloadstringtaskasync(uri); } 

i need requests run sequentially. what's proper way implement this? flag methods async , await them? wait each task using task.waitall()?

an example of i'm after:

task<string> logintask = post("login", data); // wait webrequest complete , make use of response string  // use data first request in new request: task<string> someotherrequest = get("details"); 

you can use await task.run():

await task.run(()=> { run_ui_blocking_function(); }); 

for example:

in post() function, can add this:

await task.run(()=> { poststring = post(new uri("url"), "data"); });  

and in 'get()' function, can add this:

await task.run(()=> { getstring = get(new uri("url")); });  

you'd need add async calling function. here's completed example:

private async void btn_postdata_click(object sender, eventargs e) {     await task.run(()=> { poststring = post(new uri("url"), "data"); });  } 

hope helps. it's short , sweet.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -