c# - Live Tiles not Updating -


i'm trying create background agent periodically updates user's live tiles on windows phone.

currently, code agent is:

    string = "";     private geocoordinate mycoordinate = null;     httpwebresponse webresponse;     ...     protected override void oninvoke(scheduledtask task)     {         system.diagnostics.debug.writeline("invoked");         findme();          notifycomplete();     }      private void responsecallback(iasyncresult asyncresult)     {         httpwebrequest webrequest = (httpwebrequest)asyncresult.asyncstate;         webresponse = (httpwebresponse)webrequest.endgetresponse(asyncresult);          memorystream tempstream = new memorystream();         webresponse.getresponsestream().copyto(tempstream);     }      private async void findme()     {         geolocator geolocator = new geolocator();         geolocator.desiredaccuracy = positionaccuracy.high;          try         {             geoposition currentposition = await geolocator.getgeopositionasync(timespan.fromseconds(5), timespan.fromseconds(10));              mycoordinate = new geocoordinate(currentposition.coordinate.latitude, currentposition.coordinate.longitude);              // var uri = new uri("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + mycoordinate.longitude + "&y=" + mycoordinate.latitude + "&dist=1");             // var client = new httpclient();              var webrequest = (httpwebrequest)httpwebrequest.createhttp("http://www.streetdirectory.com//api/?mode=nearby&act=location&output=json&callback=foo&start=0&limit=1&country=sg&profile=template_1&x=" + mycoordinate.longitude + "&y=" + mycoordinate.latitude + "&dist=1");             webrequest.begingetresponse(new asynccallback(responsecallback), webrequest);              system.diagnostics.debug.writeline("findme after response");             system.diagnostics.debug.writeline(mycoordinate.latitude);             system.diagnostics.debug.writeline(mycoordinate.longitude);             // var response = await client.getstringasync(uri);             system.diagnostics.debug.writeline(webresponse.tostring());              jtoken token = jarray.parse(webresponse.tostring())[0];             // jtoken token = jarray.parse(response)[0];             var name = token.next.first.first;             var address = token.next.last.first;             = name + ", " + address;         }         catch (exception)         {             system.diagnostics.debug.writeline("findme died");             = "";         }         system.diagnostics.debug.writeline("findme complete");         updateapptile();     }      private void updateapptile()     {         system.diagnostics.debug.writeline("updateapptile");         shelltile apptile = shelltile.activetiles.first();         if (apptile != null)         {             standardtiledata tiledata = new standardtiledata             {                 backcontent =             };              apptile.update(tiledata);         }         system.diagnostics.debug.writeline("update completed: " + where);     } 

when attempt run this, code reaches webrequest.begingetresponse , subsequently stops. next line, , responsecallback not reached.

an older version of code commented out, thought problem experienced same problem well.

the problem calling notifycomplete() before callback returns.

by calling notifycomplete you're telling os you've finished work , agent can terminated. isn't case when you're waiting webrequest callback.

the simple solution move call callback method. you'll need handle error exceptions , request timeout taking longer agent wait though.

changing using awaitable code may make easier you.


Comments