Reputation: 91
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
Upvotes: 5
Views: 634
Reputation: 799
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
Upvotes: 1