Windows Phone 8后台代理仅在使用调试版本时运行(似乎未在发布版本上运行)

时间:2014-07-25 10:52:37

标签: windows-phone-8 scheduled-tasks windows-phone-8.1 release-mode background-agents

这很奇怪。

当我运行调试版本时,我的Windows Phone 8应用程序的后台代理似乎全天都在更新。但是,当我更改为发布版本时,它要么根本不运行,要么很少运行。这是一个天气应用程序:我的实时图块的内容应该每小时更改一次。我已经看到它有时会每小时更新一次,但之后会停止几个小时,然后突然再次启动。应用程序的后台代理也不会被操作系统阻止,这表明后台代理没有任何问题,或者它根本没有运行多少。< / p>

目前,我在运行Windows Phone 8.1的Lumia 1020上调试并发布了应用程序版本。代码方面它们是相同的。调试版本每30分钟更新一次(我知道活动磁贴上的时间戳的原因),而发布版本在90分钟前创建时没有更新过一次(但它的后台代理仍被列为&#39;允许在Battery Saver应用程序上运行。

以下是创建代理的方法:

    private void AddAgent(string periodicTaskName)
    {
        periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask;
        if (periodicTask != null)
        {
            RemoveAgent(periodicTaskName);
        }

        periodicTask = new PeriodicTask(periodicTaskName);
        periodicTask.Description = "LiveTileHelperUpdateTask";

        try
        {
            ScheduledActionService.Add(periodicTask);

            // If debugging is enabled, use LaunchForTest to launch the agent in 5 seconds.
            //#if(DEBUG_AGENT)
               ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(1));
            //#endif
        }
        catch (InvalidOperationException)
        {

        }
        catch (SchedulerServiceException)
        {

        }
    }

这是预定的座席代码:

public class ScheduledAgent : ScheduledTaskAgent
{
    /// <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;
        });
    }

    /// 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 async override void OnInvoke(ScheduledTask task)
    {
        // If the app has not been purchased and trial has expired,
        // don't do anything here.
        if( (bool) IsolatedStorageSettings.ApplicationSettings["TrialExpired"] == true )
            return;

        // Setup view model to get data from.    
        LiveTileViewModel viewModel = new LiveTileViewModel();
        await viewModel.getWeatherForTileLocation();

        // Use a dispatcher because we are NOT on the UI thread!
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            try
            {                
                RadFlipTileData extendedData = new RadFlipTileData();
                extendedData.IsTransparencySupported = true;

                // Determine which sized tile will be the live tile.
                // Only create a live tile for it due to memory constraints.
                string liveTileSize = IsolatedStorageSettings.ApplicationSettings["LiveTileSize"].ToString();
                switch (liveTileSize)
                {
                    case "SMALL TILE":
                        extendedData.SmallVisualElement = new LiveTileSmall()
                        {
                            Icon = viewModel.Location.Hourly.Data[0].Icon,
                            Temperature = viewModel.Location.Hourly.Data[0].Temperature
                        };
                    break;
                    case "REGULAR TILE":
                        // Code here similar to small tile's
                    break;
                    default:
                        // Code here similar to small tile's
                    break;
                }

                FreeMemory();

                foreach (ShellTile tile in ShellTile.ActiveTiles)
                {
                    LiveTileHelper.UpdateTile(tile, extendedData);
                    break;
                }

                NotifyComplete();
            }
            catch (Exception e)
            {
            }
        }); 
    }
}
}

任何人都有任何想法可能导致这种情况,或者有任何类似的经验,后台代理仅适用于调试版本?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您是否尝试卸载调试版本并安装版本以验证后台任务执行?

并尝试删除发布版本的ScheduledActionService.LaunchForTest方法调用,请参阅文档中的警告部分。在当前代码中,您指定每1秒触发后台任务的测试运行。存在限制,在某些情况下,如果此时间低于60秒,则任务可能无法运行。