I have already did some POC on commnication b/w Background Task and the app it self. I was using windows universal app but it will work in silverlight phone app too.
private IBackgroundTaskRegistration timeZoonChangeTask;
public MainPage()
{
this.InitializeComponent();
NavigationHelper nvHelper = new NavigationHelper(this);
IReadOnlyDictionary<Guid, IBackgroundTaskRegistration> allTasks = BackgroundTaskRegistration.AllTasks;
if (allTasks.Count() == 0)
{
lblMessage.Text = "No Task is registered at the moment";
}
else//Task already registered
{
lblMessage.Text = "Timezoon Task is registered";
this.GetTask();
}
}
/// <summary>
/// Time zoon task registration.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Button_Click(object sender, RoutedEventArgs e)
{
await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = "MyBackgroundTask";
SystemTrigger systemTrigger = new SystemTrigger(SystemTriggerType.TimeZoneChange, false);
taskBuilder.SetTrigger(systemTrigger);
taskBuilder.TaskEntryPoint = typeof(MyBackgroundTask.TheTask).FullName;
taskBuilder.Register();
lblMessage.Text = "Timezoon Task is registered";
this.GetTask();
}
/// Get registered task and handel completed and progress changed events.
/// </summary>
private void GetTask()
{
var timeZoonChangeTask = BackgroundTaskRegistration.AllTasks.Values.FirstOrDefault();
timeZoonChangeTask.Completed += timeZoonChangeTask_Completed;
timeZoonChangeTask.Progress += timeZoonChangeTask_Progress;
}
/// <summary>
/// raised when task progress is changed app is active
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void timeZoonChangeTask_Progress(BackgroundTaskRegistration sender, BackgroundTaskProgressEventArgs args)
{
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
lblTaskProgress.Text = args.Progress.ToString() + "%";
recProgress.Width = 400 * (double.Parse(args.Progress.ToString()) / 100);
});
//this.Dispatcher.BeginInvoke(() =>
// {
// });
}
/// <summary>
/// Raised when task is completed and app is forground
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
void timeZoonChangeTask_Completed(BackgroundTaskRegistration sender, BackgroundTaskCompletedEventArgs args)
{
this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
lblMessage.Text = "Task Excecution is completed";
});
}
and below is the task class
public sealed class TheTask:IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
///Get Deferral if we are doing aysnc work. otherwise it will not work.
//Always get deferral it will not harm.
var deferral = taskInstance.GetDeferral();
taskInstance.Canceled += taskInstance_Canceled;
for (uint i = 0; i < 10; i++)
{
taskInstance.Progress = i + 10;
await Task.Delay(2000);
}
//Write last run time somewhere so the gorground app know that at when last time this backgournd app ran.
///Set this progress to show the progesss on the forground app if it is running and you want to show it.
taskInstance.Progress = 0;
deferral.Complete();
}
void taskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
}
}