c# - Unable to make asynchronous call using Windows Phone Background Task -
i have background task windows phone silverlight 8.1 app.
the background task runs fine, when put call asynchronous method in it, freezes @ stage. using debugger, stops. cannot step through code or anything.
below code:
public sealed class bgtask : ibackgroundtask { public async void run(ibackgroundtaskinstance taskinstance) { backgroundtaskdeferral _deferral = taskinstance.getdeferral(); datetime timenow = datetime.now; timespan minus28 = new timespan(0, 28, 0); datetime timegone = timenow.subtract(minus28); int resultscount = await getcount(); toasttemplatetype toasttemplate = toasttemplatetype.toasttext02; xmldocument toastxml = toastnotificationmanager.gettemplatecontent(toasttemplate); xmlnodelist textelements = toastxml.getelementsbytagname("text"); textelements[0].appendchild(toastxml.createtextnode("new pictures!")); textelements[1].appendchild(toastxml.createtextnode(resultscount + " new images in area")); toastnotificationmanager.createtoastnotifier().show(new toastnotification(toastxml)); _deferral.complete(); } private async task<int> getcount() { string page = "http://en.wikipedia.org/"; // ... use httpclient. using (httpclient client = new httpclient()) using (httpresponsemessage response = await client.getasync(page)) using (httpcontent content = response.content) { // ... read string. string result = await content.readasstringasync(); // ... display result. if (result != null && result.length >= 50) { //resultscount = 1888; } } return 9999; }
while debugging, go inside getcount() method, , stops on await getcount() section.
been trying solve days. appreciated. :)
you need define deferral @ class scope. wasy deferral requested on creation , start of backgroundtask.
public sealed class bgtask : ibackgroundtask { backgroundtaskdeferral _deferral = taskinstance.getdeferral(); public async void run(ibackgroundtaskinstance taskinstance) { //handle async stuff _deferral.complete(); } }
more information can found here: https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/create-and-register-a-background-task
Comments
Post a Comment