0
votes

I'm developing a WinRT Windows Phone 8.1 app, and I'm trying to implement a background task. After a lot of research, I was able to produce a code that should work, but when I try to run it, the debugger exits, no error, just exits... I tried with Lifecycle Events menu and by setting a timezone trigger, on both instances, i get the same result.

Details: I created a new project using Windows Runtime Component (Windows Phone) template, then edited the file to be a simple BackgroundTask:

namespace RSSReader.Tasks
{
    public sealed class GetFeed
    {
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            // Will do stuff...
        }
    }
}

Registered it on the Package.appxmanifest, on Declarations tab, created a Background Tasks declaration, ticked System event, and set the Entry point as RSSReader.Tasks.GetFeed.

Then, on the MainPage I added the following:

    private async void registerTask()
    {
        string taskName = "FeedChecker";

        var bgAccStatus = await BackgroundExecutionManager.RequestAccessAsync();

        if (bgAccStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
            bgAccStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity)
        {

            foreach (var task in BackgroundTaskRegistration.AllTasks)
            {
                if (task.Value.Name == taskName)
                {
                    return;
                }
            }

            var builder = new BackgroundTaskBuilder();
            var trigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);

            builder.Name = taskName;
            builder.TaskEntryPoint = typeof(Tasks.GetFeed).FullName;
            builder.SetTrigger(trigger);

            var registration = builder.Register();
        }
    }

It register the task, but when I change the timezone, my app is gone and the debugger exits...

1

1 Answers

0
votes

The problem is the method return. When you create a async method that returns void, it just fire-and-forget and there is no status on the method execution. For the async methods you need to check the execution status, you need to change the return from void to Task, and the app should continue it's execution after the task is triggered.

Hope it helps