如何在Windows Phone 8中每5分钟或更频繁地运行一次定期任务

在我的WP8应用程序中,我需要每5分钟下载一些json数据。
但是在MSDN中写道,定期任务每30分钟运行一次。

是否有任何变通方法可以每5分钟在后台运行一次定期任务?
没有定期后台任务,还有其他方法吗?

目前我正在使用Periodic任务下载json数据

这是我的代码

public class ScheduledAgent : ScheduledTaskAgent { public string Url { get; set; } private static FlightForNotificationDataModel _flightForNotificationData; private static NotificationDataViewModel _notificationData; public ObservableCollection Notifications { get; set; } ///  /// ScheduledAgent constructor, initializes the UnhandledException handler ///  static ScheduledAgent() { // Subscribe to the 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 to execute on Unhandled Exceptions private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger Debugger.Break(); } } ///  /// Agent that runs a scheduled task ///  ///  /// The invoked task ///  ///  /// This method is called when a periodic or resource intensive task is invoked ///  protected override void OnInvoke(ScheduledTask task) { //TODO: Add code to perform your task in background //NotificationsViewModel notificationData = new NotificationsViewModel(); //notificationData.GetData(); GetData(); Notifications = new ObservableCollection(); //NotifyComplete(); } private void GetData() { _flightForNotificationData.GenerateNotificationUrl(); if (!String.IsNullOrEmpty(_flightForNotificationData.NotificationsUrl)) { this.Url = _flightForNotificationData.NotificationsUrl; var task = new HttpGetTask(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 notVMList = new List(); 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(); } } } 

但是这个任务每30分钟运行一次

Json数据是有关航class的数据,该信息在该期间失去了实际情况

所以,我需要每隔5分钟或更频繁地运行一次

我怎样才能做到这一点?

您可以使用推送通知或rawnotification,具体取决于需要发送多少数据。 当应用程序处于前台时,您可以通过计时器完成您想要的任务吗? 但是使用后台任务是不可能的。

吐司通知

服务器端

抱歉,您不能比标准间隔更频繁地运行真正的后台任务。 但是,当应用程序在前台运行时,您可以随时获取数据,但请注意,如果需要提取大量数据,您可能会对用户造成不满。

我想这可能是你的工作。这个。

 dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0,5,0); dispatcherTimer.Start(); 

和DispatcherTimer_Tick方法

 private void dispatcherTimer_Tick(object sender, EventArgs e) { //Do your Method... } 

有关详细信息,请转到MSDN Dispatcher Timer。