通用 Windows 应用程序后台任务“已退出,代码为 1 (0x1)" [英] Universal Windows App Background Task "has exited with code 1 (0x1)"

查看:105
本文介绍了通用 Windows 应用程序后台任务“已退出,代码为 1 (0x1)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个问题上挠我的头.

Scratching my head on this one.

我的 UWP 应用程序中有一个后台任务,它注册为每 15 分钟运行一次(使用 TimeTrigger),并且每当互联网可用时(使用 SystemTrigger).我知道这些都是正确注册的,因为在使用 Visual Studio 进行调试时,它们都出现在生命周期事件"中.尽管如此,我的注册代码如下:

I've got a background task in my UWP application which is registered to run every 15 minutes (using TimeTrigger) and whenever the internet becomes available (using a SystemTrigger). I know for a fact that these are registered correctly as both appear in the "Lifecycle Events" when debugging using visual studio. Nevertheless, my code for registering them is below:

bool registered1 = false;
bool registered2 = false;
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
    if (task.Value.Name == "BackgroundGPS")
    {
        registered1 = true;
    }

    if (task.Value.Name == "InternetAvailGPS")
    {
        registered2 = true;
    }
}

await BackgroundExecutionManager.RequestAccessAsync();

if (!registered1)
{
    var builder1 = new BackgroundTaskBuilder();

    builder1.Name = "BackgroundGPS";
    builder1.TaskEntryPoint = "BackgroundTasks.BackgroundGPSTask";

    var triggerTime = new TimeTrigger(15, false);

    builder1.SetTrigger(triggerTime);

    BackgroundTaskRegistration task1 = builder1.Register();

}

if (!registered2)
{
    var builder2 = new BackgroundTaskBuilder();

    builder2.Name = "InternetAvailGPS";
    builder2.TaskEntryPoint = "BackgroundTasks.BackgroundGPSTask";

    var triggerIA = new SystemTrigger(SystemTriggerType.InternetAvailable, false);

    builder2.SetTrigger(triggerIA);

    BackgroundTaskRegistration task2 = builder2.Register();
}

我已确保在我的清单中正确声明了任务.如果不是,我的应用程序将在尝试注册它们时抛出异常.

I have ensured that the tasks are declared correctly in my manifest. If they weren't, my app would be throwing an exception when trying to register them.

如果我在调试模式下运行,我可以看到 BackgroundGPSInternetAvailGPS 都显示在生命周期事件中.但是,当我单击它们中的任何一个以强制它们执行时,我会在输出窗口中看到以下内容:

If I run in debug mode I can see that both BackgroundGPS and InternetAvailGPS are shown in the Lifecycle Events. However, when I click on either of them to force them to execute, I get the following in the output window:

The program '[4728] backgroundTaskHost.exe' has exited with code 1 (0x1).

我在后台任务的运行"方法的第一行代码中设置了一个断点,但这从未被命中.后台任务从未加载或运行,我不知道为什么.这可能不是我的 Run 方法的问题,但它看起来像这样(我省略了它的大部分内容,只包括开头和结尾)

I have a breakpoint set at the first line of code in my 'Run' method of the background task but this is never hit. The background task is never loaded nor run, and I've no idea why. This probably isn't an issue with my Run method, but it looks like this (I've omitted much of the meat of it, and just included the beginning and end)

public async void Run(IBackgroundTaskInstance taskInstance)
{
    Debug.WriteLine("GPS Started");
    int errCode = 0;
    try
    {
        _deferral = taskInstance.GetDeferral();
        saveGPSStatus(DateTime.Now.ToString(), "", " ");

        var access = await Geolocator.RequestAccessAsync();

        if (access != GeolocationAccessStatus.Allowed)
        {
            Debug.WriteLine("No access");
            saveGPSStatus("", "", "No GPS Access");
            return;
        }

        Geolocator locator = new Geolocator();
        locator.DesiredAccuracyInMeters = 100;

        Geoposition position = await locator.GetGeopositionAsync();

         //Stuff goes on in here

        _deferral.Complete();
    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message);
        saveGPSStatus("", "", "Unexpected error. Err code "+errCode+". " + e.Message);
        _deferral.Complete();
    }

}

我在运行版本 10.0.10586.164 的 Windows Phone 和 Windows Tablet 上都尝试过这个,但它们都没有执行我的后台任务.

I have tried this both on a Windows Phone and a Windows Tablet both running build 10.0.10586.164, and they both do not execute my background task.

正如我上面所说的,这些在几周前运行良好,直到应用程序的一些用户一直在抱怨时才意识到出了点问题!

As I said above, these were working perfectly a few weeks ago and have only realised something was wrong as some of the app's users have been complaining!

非常感谢任何帮助.

推荐答案

最后让它工作了,我只是在我的主项目中缺少对我的后台任务项目的引用.我一定是在那之前的某个时候不小心删除了它.

Got it working in the end, I was simply missing a reference to my background task project within my main project. I must have deleted it by accident at some point before that.

这篇关于通用 Windows 应用程序后台任务“已退出,代码为 1 (0x1)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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