c# - separating asynchronous HttpClient and Ui thread Winrt? -


i trying download json data rest service , i'm doing button click navigating new page , in new page i'm using async methods download data.

but still ui gets stuck ... ui get's stuck in first page until download completed.. i've tried using async on pageloaded event .. , in many other loaded events on other elements of page .. how separate them both?

 private async void page_loaded(object sender, routedeventargs e)     {         //download helper         iwebservice _downloadclient = new downloadclient();         var _jsonstring = _downloadclient.getdataasync(uri);         //json object converter         ijsondeserializer _deserializer = new jsontoobject();         apodprop = _deserializer.desrializeto<rootobject>(_jsonstring);           apobinding(); //sets textblocks data downloaded     } 

the whole page doesn't appear until download completed , didn't use await because internal methods in download client use async , await keywords ..

download client edited :

public task<string> getdataasync(string uri)         {             //throw new notimplementedexception();             try             {                 using (httpclient client = new httpclient())                 {                     task<httpresponsemessage> _result = client.getasync(uri);                     httpresponsemessage responsedata = _result.result;                     return await responsedata.content.readasstringasync();                 }             }             catch (httprequestexception)             {                  messagedialog md = new messagedialog("http exception");                await md.showasync();             }              return null;         } 

update: ui doesnt show until download complete.

 protected override async void onnavigatedto(navigationeventargs e)     {         hardwarebuttons.backpressed += hardwarebuttons_backpressed;         await mytask();         } 

mytask:

private async task mytask()         {             iapiservice _apiprovider = new restclient.apiservice.nasaapod();             var formattablestring = _apiprovider.getformattableapicallstring("api_key;concept_tags");              iparameterfiller filler = new parameterfiller();             var uri = filler.fillparameters(formattablestring, "i3pa7rdskvrer6hm6f51bhwzcmojm6alq6bjbbeu;true");              iwebservice _downloadclient = new downloadclient();             var _jsonstring = await _downloadclient.getdataasync(uri);              ijsondeserializer _deserializer = new jsontoobject();             apodprop = _deserializer.desrializeto<rootobject>(_jsonstring);               apobinding();         } 

finally nailed it

await task.run(()=> { mytask();  }  );          apobinding(); 

that's all need ...

now concept part ... in windows there rule controls must accessed thread created , code contains ui controls must run on same thread unless explicitly specify ...

in first case the ui thread i.e apobinding along downloading code whole ui thread freezed .

in order responsiveness had put downloading on separate process.

then i've experimented placing ui inside mytask , task.run .. both ways resulted in similar o/p .. yes guessed according windows rule of threads if ui control accesse other thread created result in crash crash crash

i had real tough time responsiveness ... don't forget user uninstall if application unresponsive vs or windows or mac or linux or other great software.. make ui responsive , become best (being sarcastic .. there lot more do).

in comments _downloadclient.getdataasync isn't async , interface method isn't either. 2 options:

  1. make interface method return task.
  2. put synchronous download on thread-pool: await task.run(() => _downloadclient.getdataasync(uri));. quick fix. if there few downloads running @ same time not perf or memory problem , valid fix.

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -