c# - Asynchronous Methods in Wp8 app -


my application retrieves data server asynchronous calls apis. works fine long user remains in app. after implementing fast app resume, when app resumes after clicking on tile, control comes page on user left previously.

if there asynchronous call running when user had deactivated app(hit start button previously), throws following exception.exception.

in fact exception thrown following code.

    private async task<string> httprequest(httpwebrequest request)     {         string received;          using (var response = (httpwebresponse)(await task<webresponse>.factory             .fromasync(request.begingetresponse, request.endgetresponse, null)))         {             using (var responsestream = response.getresponsestream())             {                 using (var sr = new streamreader(responsestream))                 {                     //cookiejar = request.cookiecontainer;                     //responsecookies = response.cookies;                     received = await sr.readtoendasync();                 }             }         }          return received.replace("[{}]", "[]")                 .replace("[{},{}]", "[]")                 .replace("\"subcat_id\":\"\"", "\"subcat_id\":\"0\"");     }  

is there way stop execution of async method on onnavigatedfrom method when user deactivates app? or there way preserve state of async call , resume again?

thanks in advance.

when app deactivated proccesses stopped (msdn):

when user navigates forward, away app, after deactivated event raised, operating system attempt put app dormant state. in state, of application’s threads stopped , no processing takes place, application remains intact in memory.

your async method should allow cancellation - here have example of cancelling.

use cancellationtokensource , in deactivation event or onnavigatedfrom put:

if (cts != null) cts.cancel(); 

in case should implement asyncreading (via buffer) response enable token.throwifcancealltionrequested(). can here - method 3 , here. maybe help.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -