这是一个奇怪的问题。
当我运行调试版本时,我的 Windows Phone 8 应用程序的后台代理似乎全天都在正常更新。但是,当我更改为发布版本时,它要么根本不运行,要么很少运行。这是一个天气应用程序:我的动态磁贴的内容应该每小时更改一次。我见过它有时每小时更新一次,然后停止几个小时,然后突然重新开始。应用程序的后台代理在任何时候都不会被操作系统阻止,这对我来说表明后台代理没有任何问题,或者它只是没有运行太多/根本没有运行。
目前,我已经在运行 Windows Phone 8.1 的 Lumia 1020 上调试和发布了应用程序版本。在代码方面它们是相同的。调试版本每 30 分钟更新一次(我知道活动磁贴上时间戳的原因),而发布版本自 90 分钟前创建以来没有更新一次(但其后台代理仍被列为“允许”在省电应用程序上运行)。
这是创建代理的方法:
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)
{
}
});
}
}
}
任何人都知道可能导致这种情况的任何想法,或者有任何类似的经验,即后台代理仅适用于调试版本?
谢谢你的时间。