Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

c# - Universal Windows App Background Task "has exited with code 1 (0x1)"

Scratching my head on this one.

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.

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).

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();
    }

}

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!

Any help is greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...