For a Windows 8 App, in C#/Xaml I try to register a background task. It's hard to tell but I guess my background task is well registred but when I click on the name of my background task on the Debug Location Toolbar my app stops working without any message.
I looked at the log on the Event Viewer and I get : "The background task with entry point MyApp.Pages.SampleBackgroundTask and name Example hourly background task failed to activate with error code 0x80010008."
Here is my code :
private async void Button_Click_BackgroundTask(object sender, RoutedEventArgs e)
{
string entryPoint = "MyApp.Pages.SampleBackgroundTask";
string taskName = "Example hourly background task";
TimeTrigger hourlyTrigger = new TimeTrigger(60, false);
BackgroundTaskRegistration myBackGroundTaskRegistration = RegisterBackgroundTask(entryPoint, taskName, hourlyTrigger, null);
if(myBackGroundTaskRegistration != null)
AttachProgressAndCompletedHandlers(myBackGroundTaskRegistration);
}
public static BackgroundTaskRegistration RegisterBackgroundTask(String taskEntryPoint,
String name,
IBackgroundTrigger trigger,
IBackgroundCondition condition)
{
var taskRegistered = false;
var iter = Windows.ApplicationModel.Background.BackgroundTaskRegistration.AllTasks;
foreach (var item in iter)
{
var task = item.Value;
if (task.Name == name)
{
taskRegistered = true;
return item.Value as BackgroundTaskRegistration;
}
}
var builder = new BackgroundTaskBuilder();
builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(trigger);
if (condition != null)
{
builder.AddCondition(condition);
}
BackgroundTaskRegistration taskBack = builder.Register();
return taskBack;
}
private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task)
{
task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
}
private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
var progress = "Progress: " + args.Progress + "%";
}
private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
Debug.WriteLine("Hi");
}
I added this in the manifest :
<Extension Category="windows.backgroundTasks" EntryPoint="MyApp.Pages.SampleBackgroundTask">
<BackgroundTasks>
<Task Type="timer" />
</BackgroundTasks>
</Extension>
Did I forget something?
Thank you for your help.
EDIT : here is the code of my Task, within one of my project page :
public sealed class SampleBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
_deferral.Complete();
}
}
I had this problem too. I also had mine in the same project. I finally got it to work by moving the task into a separate project. I had to set the project output as windows runtime component(under properties applicaiton tab) . Also don't forget to add the project as a reference.
You need user permission before you can run a BackgroundTask you can either do this manually: by opening the app and clicking Permissions in the Settings Charm and turning the Lock screen setting to on.
You can also prompt the user when the first screen is loaded with this:
BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
Needless to say the latter is going to lead to a much better user experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With