c# - HttpClientt.SendAsync doesnt wait/lock execution -




var httpresponsemessage = await httpclient.sendasync(message).configureawait(false); var datastream = await httpresponsemessage.content.readasstreamasync(); 

this idea should awaited, no matter executions exists method , returns ui. execution resumes when responses arrives, time ui has updated execution finished, when in fact hasn't. calling methods awaited.

initial method not awaited design (task.run(() => startdownload(selectedschedules)); starts ui method executing services triggers httpclient, when call finished ui should update progress, second httpclient.sendasyncis executed, execution returns ui

task.run(() => startdownload(selectedschedules)); //first call, initiated button  public async task startdownload(scheduleslist list) {     //var t = new task(() => _scheduleservices.download(list));     //t.start();     //await t;     await _scheduleservices.downloadiwcfdb(list, usbinfomodels); }  public async task download(scheduleslist scheduleslist) {                              await downloaddb(scheduleslist);         }  private async task downloaddb(scheduleslist scheduleslist) {     using (var httpclient = new httpclient())     {         var message = new httprequestmessage(new httpmethod("post"), apicallurls.getiwcfschedules)         {             content = new stringcontent(jsonconvert.serializeobject(scheduleslist), encoding.utf8, "application/json")         };          httpclient.timeout = timespan.fromminutes(20);         var httpresponsemessage= await httpclient.sendasync(message).configureawait(false);         var datastream = await httpresponsemessage.content.readasstreamasync();         using (stream contentstream = datastream, stream = new filestream(path.combine(directories.somedir, directories.somefilename), filemode.create, fileaccess.write, fileshare.none))         {             await contentstream.copytoasync(stream);         }     } } 

call chain added, irrelevant code removed methods

you're problem lies within first call.

in code have:

task.run(()=>startdownload(selectedschedules)); //first call, initiated button //i assume afterwards update progressbar or give other progress feedback 

what is: calls startdownload , immediately continues execution. other stuff (downloading etc) happening in background. remember method startdownload not block; returns task object. if not await task object, code proceed.

i guess wanted is: call startdownload, wait finish, update progress.

a quick solution mark event handler of button async, , use async way. method bit this:

private async void handleevent() {     await startdownload();     //update progress } 

i can recommend blog post stephen cleary introduction async-await: https://blog.stephencleary.com/2012/02/async-and-await.html





wiki

Comments

Popular posts from this blog

elasticsearch - what is the equivalent data type for geo_point in hibernate search? -

firebase - How to wait value in Ionic 2 -

Jenkins: find build number for git commit -