c# - 在我的后台任务中查找辅助磁贴失败,代码为 1

标签 c# windows-8 background windows-runtime windows-store-apps

我想通过后台任务更新我的辅助磁贴。我的问题是,使用 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/

相关文章:

c# - FlexLayout 可绑定(bind)源 -xamarin 表单 - 如何在不绑定(bind)的情况下获取数据

c# - 如何在c#中为字符串添加单引号?

c# - 在项目之外访问图像的最佳方式?

c# - 为什么必须使用 "out"而不是 ref?

windows-8 - Windows Phone 8.1 WINRT 应用程序中何时触发激活的事件

windows-8 - Windows 8 Live Tile 的定期更新

android - 在代码中编辑按钮形状(描边、渐变)

xaml - 相当于 WinRT 中的可编辑组合框?

android : when using MediaPlayer to play background music, 音效过早停止

css - 如何将 div/行与流体背景颜色混合