如何在 Windows Phone 8 中每 5 分钟或更频繁地运行周期性任务 [英] how to run periodic tasks every 5 minutes or more frequently in windows phone 8

查看:13
本文介绍了如何在 Windows Phone 8 中每 5 分钟或更频繁地运行周期性任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

是否有任何解决方法可以每 5 分钟在后台运行一次周期性任务?
如果没有定期的后台任务,是否还有其他方法可以做到这一点?

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?

目前我正在使用周期性任务来下载 json 数据

Currently I'm using Periodic task to download json data

这是我的代码

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 分钟运行一次

But this task runs every 30 minutes

Json 数据是关于航班的数据,该信息在那个时期失去其真实性

Json data is data about flights and that information loses its actuality in that period

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

So, I need to make it run every 5 minutes or more frequently

我该怎么做?

推荐答案

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

You can use push Notification, or rawnotification dependent on how much data is needed to be send. And when the app is in foreground you can accomplish what you want with a timer? But using background tasks is not possible.

Toast 通知

服务器端

这篇关于如何在 Windows Phone 8 中每 5 分钟或更频繁地运行周期性任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆