【发布时间】:2014-04-14 11:20:05
【问题描述】:
在我的 WP8 应用程序中,我需要每 5 分钟下载一些 json 数据。
但在 MSDN 中,定期任务每 30 分钟运行一次。
是否有任何解决方法可以每 5 分钟在后台运行定期任务?
在没有定期后台任务的情况下,还有其他方法可以做到这一点吗?
目前我正在使用定期任务来下载 json 数据
这是我的代码
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 the UnhandledException handler
/// </remarks>
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();
}
}
/// <summary>
/// Agent that runs a scheduled task
/// </summary>
/// <param name="task">
/// The invoked task
/// </param>
/// <remarks>
/// This method is called when a periodic or resource intensive task is invoked
/// </remarks>
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<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();
}
}
}
但此任务每 30 分钟运行一次
Json 数据是有关航班的数据,并且该信息在该期间失去了真实性
所以,我需要让它每 5 分钟或更频繁地运行一次
我该怎么做?
【问题讨论】:
标签: c# windows-phone-8