我想通过后台任务更新我的辅助磁贴。我的问题是,使用 SecondaryTile.FindAllAsync();
遍历所有固定的图 block 失败并使用代码 1 退出我的后台任务。
我不知道为什么不能这样做,为什么我没有得到任何异常以及为什么这在主应用程序中有效。这可能是内存问题吗?
我的代码是这样的:
public sealed class SecondaryTileUpdater : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
var list = await SecondaryTile.FindAllAsync(); // Here it fails :(
foreach (SecondaryTile liveTile in list)
{
// Update Secondary Tiles
// (...)
}
}
}
这是我得到的错误:
The program '[5644] backgroundTaskHost.exe: Managed (v4.0.30319)' has exited with code 1 (0x1).
任何想法,这可能是什么原因? 感谢您的帮助!
最佳答案
一旦您的异步方法等待,您的任务就结束了。请求延迟,让您的异步代码有时间执行。
public sealed class SecondaryTileUpdater : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
//HERE: request a deferral
var deferral = taskInstance.GetDeferral();
var list = await SecondaryTile.FindAllAsync(); // Here it fails :(
foreach (SecondaryTile liveTile in list)
{
// Update Secondary Tiles
// (...)
}
//HERE: indicate you are done with your async operations
deferral.Complete();
}
}
关于c# - 在我的后台任务中查找辅助磁贴失败,代码为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14477476/