Search code examples
c#windows-store-appswindows-rtbackground-taskwindows-8.1-universal

Windows RT - Background Task Not Firing


I am implementing a background task for my Windows Store Application (Win 8.1 App). I have written a very simple test class, registered it, prompted for access, but when I choose to debug the task from the debug toolbar, nothing happens. I have also waited 15 minutes multiple times today and it does not output anything. Yet, it shows (from the code perspective) that the Task is registered and I am not getting any exceptions generated.

The Background Task:

    public sealed class BGFunMessage : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background " + taskInstance.Task.Name + " Starting...");
        }
    }

The Register Class:

    public class RegisterWorkers
{
    public static async void Run()
    {
        var taskRegistered = false;
        var taskName = "BGFunMessage";
        BackgroundTaskRegistration bTask = null;

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == taskName)
            {
                //taskRegistered = true;
                bTask = (BackgroundTaskRegistration)(task.Value);
                bTask.Unregister(true);
                break;
            }
        }

        if (!taskRegistered)
        {
            string entryPoint = typeof(BGFunMessage).FullName;

            bTask = RegisterBackgroundTask(entryPoint, taskName);
        }
    }

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, string name)
    {
        var builder = new BackgroundTaskBuilder();

        builder.Name = name;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(new TimeTrigger(15, false));

        BackgroundTaskRegistration task = null;

        try
        {
            task = builder.Register();
        }
        catch (Exception ex)
        {
            LiveLog.WriteException(ex, LogType.WARNING);
        }

        return task;
    }
}

How I call it from a page in my app:

RegisterWorkers.Run();

I have tried following multiple tutorials, that all mostly say the same thing. I am also using the MSDN Samples downloaded from GitHub and I don't see anything on the surface that makes their code any different from mine (apart from that their Register method returns a Task<>). I am able to debug the Sample project background tasks, but not my own. Is there something I am doing incorrectly here? Thanks.


Solution

  • After many hours of troubleshooting, I have found the solution to my problem. I am unsure of why this must be the case, but it works. Originally (unspecified above), I had the Background Task in the same project as my app. The ONLY difference I could find across the board was that everywhere I seen, the background tasks were in a WINDOWS RUNTIME COMPONENT project. I pulled my code out and into its own project, and referenced the .DLL and now it all works fine.

    I should note however, if anyone ever needs this fix--that now I no longer have access to any of my data or SyncContext from Azure Mobile Services as it currently stands. I have no idea what I am going to have to re-architect to make it work now, but the issue above is now resolved.

    I will assume, that you should always have a Shared Code library project that your main app project should reference, that way my Background Task can also reference the shared project and I can still have access to my models and other data.