c# - how to run periodic tasks every 5 minutes or more frequently in windows phone 8 -
in wp8 application need download json data every 5 minutes.
in msdn it's written periodic tasks run every 30 minutes.
are there workarounds run periodic tasks in background every 5 minutes?
there other ways of doing without periodic background tasks?
currently i'm using periodic task download json data
here code
public class scheduledagent : scheduledtaskagent { public string url { get; set; } private static flightfornotificationdatamodel _flightfornotificationdata; private static notificationdataviewmodel _notificationdata; public observablecollection<notificationviewmodel> notifications { get; set; } /// <remarks> /// scheduledagent constructor, initializes unhandledexception handler /// </remarks> static scheduledagent() { // subscribe managed exception handler deployment.current.dispatcher.begininvoke(delegate { application.current.unhandledexception += unhandledexception; }); _flightfornotificationdata = new flightfornotificationdatamodel("isostore:/tashkentair.sdf"); _notificationdata = new notificationdataviewmodel("isostore:/tashkentair.sdf"); } /// code execute on unhandled exceptions private static void unhandledexception(object sender, applicationunhandledexceptioneventargs e) { if (debugger.isattached) { // unhandled exception has occurred; break debugger debugger.break(); } } /// <summary> /// agent runs scheduled task /// </summary> /// <param name="task"> /// invoked task /// </param> /// <remarks> /// method called when periodic or resource intensive task invoked /// </remarks> protected override void oninvoke(scheduledtask task) { //todo: add code perform task in background //notificationsviewmodel notificationdata = new notificationsviewmodel(); //notificationdata.getdata(); getdata(); notifications = new observablecollection<notificationviewmodel>(); //notifycomplete(); } private void getdata() { _flightfornotificationdata.generatenotificationurl(); if (!string.isnullorempty(_flightfornotificationdata.notificationsurl)) { this.url = _flightfornotificationdata.notificationsurl; var task = new httpgettask<notifications>(this.url, onpostexecute); task.execute(); } else return; } private void onpostexecute(notifications responseobject) { this.onnotificationsdownloaded(responseobject); notifycomplete(); } private void onnotificationsdownloaded(notifications notifications) { if (string.isnullorempty(notifications.hasdata)) { notifications.clear(); list<notificationviewmodel> notvmlist = new list<notificationviewmodel>(); foreach (tashkentair.models.notification not in notifications.notifications_) { notificationviewmodel notvm = new notificationviewmodel(); notvm.date = not.date; notvm.direction = not.direction; notvm.flight_ = not.flight_; notvm.time = not.time; notvm.timestamp = not.timestamp; switch (not.status) { case 0: notvm.status = "Нет данных"; break; case 1: notvm.status = "Прибыл"; _flightfornotificationdata.deleteflightfornotification(not.flightid); break; case 2: notvm.status = "Отправлен"; break; case 3: notvm.status = "Регистрация"; break; case 4: notvm.status = "Посадка"; break; case 5: notvm.status = "Задержан"; break; case 6: notvm.status = "Отменен"; break; default: notvm.status = ""; break; } shelltoast toast = new shelltoast(); toast.title = notvm.flight_; toast.content = notvm.status; toast.show(); notvmlist.add(notvm); } notvmlist = notvmlist.orderby(n => n.timestamp).tolist(); notvmlist.foreach(this.notifications.add); if (_notificationdata == null) _notificationdata = new notificationdataviewmodel("isostore:/tashkentair.sdf"); _notificationdata.savejsonnotificationstodb(notifications); } else { _flightfornotificationdata.clearalldata(); _notificationdata.clearalldata(); } } }
but task runs every 30 minutes
json data data flights , information loses actuality in period
so, need make run every 5 minutes or more frequently
how can that?
you can use push notification, or rawnotification dependent on how data needed send. , when app in foreground can accomplish want timer? using background tasks not possible.
Comments
Post a Comment