In my WP8 application I need to download some json data every 5 minutes.
But in MSDN it's written that periodic tasks are run every 30 minutes.
Are there any workarounds to run periodic tasks in background every 5 minutes?
Are there any other ways of doing that without periodic background tasks?
Currently I'm using Periodic task to download json data
Here is my 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 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();
}
}
}
But this task runs every 30 minutes
Json data is data about flights and that information loses its actuality in that period
So, I need to make it run every 5 minutes or more frequently
How can I do that?