//Add
internet condition
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
Change the Task trigger to be One Shot
//waits
15 minutes to schedual the background task, once scedualed it
//will
fire within 15 minutes once
builder.SetTrigger(new TimeTrigger(15, true));Add a callback to our task
taskRegistration.Completed
+= taskRegistration_Completed;
next create the callback, on the view I created a Text box named Output so that I could display my results to the user. Now since there really is no way to directly pass a result back to your application from your background task i utilize the Application data store to do this.
private async void taskRegistration_Completed(BackgroundTaskRegistration sender,
BackgroundTaskCompletedEventArgs args)
{
await CoreApplication.MainView.CoreWindow.Dispatcher
.RunAsync(CoreDispatcherPriority.Normal,
() => Output.Text = "Task Fired");
if (sender.Name == "TimerTask")
try
{
args.CheckResult();
var localSettings = ApplicationData.Current.LocalSettings;
var data = localSettings.Values["data"].ToString();
await CoreApplication.MainView.CoreWindow.Dispatcher
.RunAsync(CoreDispatcherPriority.Normal,
() =>
Output.Text = data);
}
catch (Exception ex)
{
var data = String.Format("{0}
Error:{1}", Output.Text, ex.Message);
CoreApplication.MainView.CoreWindow.Dispatcher
.RunAsync(CoreDispatcherPriority.Normal,
() => Output.Text =
data).AsTask().RunSynchronously();
}
}
using System;
using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
namespace pc.BackgroundTimerTask
{
public sealed class TimerTask :IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var d = taskInstance.GetDeferral();
this.SendToast("Data
Refreshed");
string[] data = new string[1];
//Deliberate Error
//data[1] = DateTime.Now.ToString();
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["data"] = DateTime.Now.ToString();
d.Complete();
}
private void SendToast(string Message)
{
var ToastXml = ToastNotificationManager
.GetTemplateContent(ToastTemplateType.ToastText01);
//extract
all the text elements
var toastTextElements = ToastXml.GetElementsByTagName("text");
//set
the one text element we have to a value
toastTextElements[0].AppendChild(ToastXml.CreateTextNode(Message));
//create
the toast
var Toast = new ToastNotification(ToastXml);
//show
the toast in the top right corner
ToastNotificationManager.CreateToastNotifier().Show(Toast);
}
}
}
as you may notice I refactored our Background task a little bit, and pass my value back to my application using the Application data store. now I also, tried createing an exception within my application to try and handle it on the completed callback, but I haven't been able to figure this out, it seems that the background task throws my exception and the callback never fires.
now one thing to note is that our callback will only fire while our app is running, however a background task will fire whether the app is running, suspended or even terminated. so keep in mind that our TaskCompleted event will then fire next time we activate our application.
pretty cool eh?