Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - Windows Phone 8.1 Background Task - Can't Debug and won't fire

Im having a issue with the Background Tasks in WP8.1 I have created a background task as a windows runtime component following this tutorial : http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977055.aspx

The problem is, i can't get my background task to run. It runs onNetworkChange. When i can to flight mode and back it is not firing. When i go to lifecycle events in the Debug Location toolbar it says No Background tasks. I have debugged the code that registers the background task and it is getting registered. I am also getting 'This breakpoint will not currently be hit. No symbols have been loaded for this document' which i think is causing the problem.

I have tried - deleting the bin and obj folder and rebuilding. - cleaning the project. - trying to build the project from scratch. - turning Just my code option off. - tried doing the same thing on another machine, still nothing.

My code for registering

var taskRegistered = false;
        var exampleTaskName = "UploadTask";

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            if (task.Value.Name == exampleTaskName)
            {
                taskRegistered = true;
                break;
            }
        }

        if (!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = exampleTaskName;
            builder.TaskEntryPoint = "Tasks.Upload";
            builder.SetTrigger(new SystemTrigger(SystemTriggerType.NetworkStateChange, false));
            BackgroundTaskRegistration task = builder.Register();
        }

package manifest file is as follows

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="Tasks.Upload">
      <BackgroundTasks>
        <Task Type="systemEvent" />
        <m2:Task Type="deviceUse" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

My task looks like this :

namespace Tasks
{
public sealed class Upload : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Am i even getting here?");
    }
  }
}

Can anyone help as i've spent far too long getting this to work. Thanks

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

As I've tried your code, there is a problem with this specific SystemTriggerType.NetworkStateChange - indeed I also don't see the registered BackgroundTask in Lifecycle Events dropdown. But if I only change the SystemTriggerType for example to SystemTriggerType.TimeZoneChange then I'm able to see it.

Here is the code modified a little:

await BackgroundExecutionManager.RequestAccessAsync();
if (!taskRegistered)
{
    Debug.WriteLine("Registering task inside");
    var builder = new BackgroundTaskBuilder();
    builder.Name = exampleTaskName;
    builder.TaskEntryPoint = "Tasks.Upload";
    builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
    BackgroundTaskRegistration task = builder.Register();
    await new MessageDialog("Task registered!").ShowAsync();
}

I'm not sure why with the original code the BackgroundTask is not visible in VS, though it is being registered - it's in BackgroundTaskRegistration.AllTasks - in this case maybe try to debug with different SystemTriggerType and swich to desired one with release version.

I've also tested if the BackgroundTask with the problematic SystemTriggerType.NetworkStateChange works - and indeed - it is working. I've modified your BackgroundTask a little to send a toast message when NetworkState changes. After registering the task, when I turn the WiFi on/off, I get a toast messgae. The code for the task:

public sealed class Upload : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Hello Pat");
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList textElements = toastXml.GetElementsByTagName("text");
        textElements[0].AppendChild(toastXml.CreateTextNode("Upload Task - Yeah"));
        textElements[1].AppendChild(toastXml.CreateTextNode("I'm message from your Upload task!"));
        ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
    }
}

The complete example you can download here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...