dimanche 29 mars 2015

Background Task periodic


Vote count:

0




I'm using C# and Windows Phone 8.1 as Universal App. I have an application which can runs in the background. Here is my Background class:



public sealed class RTask : IBackgroundTask
{
BackgroundTaskDeferral deferral_;
public async void Run(IBackgroundTaskInstance taskInstance)
{
Debug.WriteLine("Background started");
deferral_ = taskInstance.GetDeferral();
Debug.WriteLine("GetDeferral");


Debug.WriteLine("Retrieving data from url...");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://mysite"));
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
string pageSource = new StreamReader(response.GetResponseStream()).ReadToEnd();
Debug.WriteLine("PageSource Retrieved");
pageSource = (System.Net.WebUtility.HtmlDecode(pageSource));
pageSource = pageSource.Replace("\"", "'");
//ParseHtml(pageSource);
deferral_.Complete();
}
}


Here is my register code for register background:



private async void RegisterBtn_Click(object sender, RoutedEventArgs e)
{
string myTaskName = "RTask";
foreach (var cur in BackgroundTaskRegistration.AllTasks)
{
Debug.WriteLine("cur.Value.Name: " + cur.Value.Name);
if (cur.Value.Name == myTaskName)
{
await (new MessageDialog("Task already registered")).ShowAsync();
return;
}
}
await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder { Name = "RTask", TaskEntryPoint = "MyTask.RTask" };
taskBuilder.SetTrigger(new TimeTrigger(15, true));
BackgroundTaskRegistration myFirstTask = taskBuilder.Register();

await (new MessageDialog("Task registered")).ShowAsync();
}


I need every 1 hour this background run and retrieving my data. Is this possible ? by according the code It's can't use periodic! I'v tested in the phone and just one time is working and then nothings can do. If I didn't complete the deferral, is wrong with it?


In the Microsoft example of Background Task, they used a code 'ThreadPoolTimer'. in my case I need this or not?


Thannks



asked 1 min ago







Background Task periodic

Aucun commentaire:

Enregistrer un commentaire