Search code examples
c#windows-8windows-runtimewindows-store-appsbackground-task

Windows Store Background Task


I'm trying to test my background task:

This is my task registration:

    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        foreach (var task in BackgroundTaskRegistration.AllTasks)
        {
            task.Value.Unregister(true);
        }
        await BackgroundExecutionManager.RequestAccessAsync();
        var builder = new BackgroundTaskBuilder();
        builder.Name = "MySampleTask";
        builder.TaskEntryPoint = "TestApplication.MyBackgroundTask";
        builder.SetTrigger(new TimeTrigger(15,false));
        var ret = builder.Register();
    }

And here is my BackGround Task class:

namespace TestApplication
{
public sealed class MyBackgroundTask : IBackgroundTask
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        FetchDataFromServerAsync();
    }

    public async Task FetchDataFromServerAsync()
    {
        if (Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile() != null)
        {
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync(@"http://www.inbox.lv");
            response.EnsureSuccessStatusCode();
            string responseBodyAsText = await response.Content.ReadAsStringAsync();
            await FileManager.FlushDataToFileAsync(responseBodyAsText, DateTime.Now.ToString("dd.MM.yyyy_HH.mm.ss") + ".txt");
        }
    }
}

}

Here is my manifest:

<Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="TestApplication.MyBackgroundTask">
      <BackgroundTasks>
        <Task Type="timer" />
      </BackgroundTasks>
    </Extension>
  </Extensions>

When i trigger the background task via Debug location - my app just closes and non of the breakpoints gets hit.


Solution

  • Sample applications

    There are a lot of sample applications made by Microsoft which show how to use Windows Store specific API's. The background task is one of them. You can go ahead and Download all of them on code.msdn.microsoft.com or Download the background task sample only from the site

    Background task in another project

    I have read a little bit about background tasks a while ago and I do remember a mention about your task needing to be in another project. Indeed when you open the sample project and look at the solution explorer you can see straight away that it is the case.

    enter image description here

    Notice how the file SampleBackgroundTask.vb is situated in Tasks which is a second project.

    (Don't be scared off by the vb file there are C# samples also)

    My recommendation to get your background task done

    So far I have gone over multiple Windows Runtime API samples to get my work done and they all worked.

    I'd say your best bet is to go through the code of the sample application I linked above and see what is different between your code and the sample's one.

    Best of luck. Leave a comment if you need more specifications and I can take a further look into background tasks myself to help you out.