4
votes

I want create a Background task which starts when the application starts. For that I use Application Trigger.

MainPage.xaml.cs

    var trigger = new ApplicationTrigger();
    BackgroundManagement.RegisterBackgroundTask("InternetBackgroundTask.InternetBackground", "Internet", trigger, null);
    await trigger.RequestAsync();

BackgroundManagement.cs

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint,string taskName,IBackgroundTrigger trigger,IBackgroundCondition condition)
    {
        //
        // Check for existing registrations of this background task.
        //

        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {

            if (cur.Value.Name == taskName)
            {
                //
                // The task is already registered.
                //

                return (BackgroundTaskRegistration)(cur.Value);
            }
        }

        //
        // Register the background task.
        //

        var builder = new BackgroundTaskBuilder();

        builder.Name = taskName;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);
        

        if (condition != null)
        {

            builder.AddCondition(condition);
        }

        BackgroundTaskRegistration task = builder.Register();
       
        return task;
    }

Mytask on another project

namespace InternetBackgroundTask
{
public sealed class InternetBackground : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        System.Diagnostics.Debug.WriteLine("Run Background Task");
    }
}

So when I launch my application I have this error :

Exception thrown at 0x776BE26B (KernelBase.dll) in backgroundTaskHost.exe: 0x04242420 (parameters: 0x31415927, 0x5DE30000, 0x003CED68).

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.

I've referenced my background Task in my project and I've added my background Task in my manifest

1
Have you referenced your windows runtime component containing your background task in your other project? - TableCreek
Yes it's referenced. - Guinard Arnaud
Do you have the option to change the os language to english? My french is too bad to translate it to the english equivalent words. The error message sounds a bit familiar to me I think. Have you also tried unregistering the task before returning / creating it new? - TableCreek
I change error message french to english. Yes I tried to unregistering, I even uninstall and install my application. - Guinard Arnaud
Please send it to me in english. - TableCreek

1 Answers

1
votes

There are two things wich are not obvious from your snipped:

Have you declared your background task under "Declarations" in your Package.appxmanifest?

And second: In what project type is "InternetBackground.cs"? It should be a Windows Runtime Component