1

I'm using C# and Windows Phone 8.1 as Universal App.

I used background task by this article: http://www.romasz.net/how-to-add-a-backgroundtask/

I want when my application is in background, open a site and grab html source. I've used this code (in the background project):

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Background started\nRetrieving data");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://mysite"));
        HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
        string pageSource = new StreamReader(response.GetResponseStream()).ReadToEnd();
        pageSource = (System.Net.WebUtility.HtmlDecode(pageSource));
        Debug.WriteLine("source:  " +  pageSource);
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList textElements = toastXml.GetElementsByTagName("text");
        textElements[0].AppendChild(toastXml.CreateTextNode("My first Task - Yeah"));
        textElements[1].AppendChild(toastXml.CreateTextNode("source: " + pageSource));
        ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(toastXml));
    }

Here is my debug output for background:

'BACKGROUNDTASKHOST.EXE' (CoreCLR: DefaultDomain): Loaded 'C:\windows\system32\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\Data\SharedData\PhoneTools\AppxLayouts\cc0fe12e-67d1-47c2-be7b-58b7bf08691fVS.Debug_AnyCPU.myUser\MyTask.winmd'. Symbols loaded.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.RUNTIME.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\WinMetadata\Windows.winmd'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.THREADING.TASKS.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Net.Requests.ni.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Net.ni.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.IO.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.RUNTIME.EXTENSIONS.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\SYSTEM.DIAGNOSTICS.DEBUG.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Background started
Retrieving data
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\System.Runtime.WindowsRuntime.NI.DLL'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'BACKGROUNDTASKHOST.EXE' (CoreCLR: .): Loaded 'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
The program '[3620] BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1).

It's fired but can't access to my website. Please help me.

Thank you.

  • have you tried to connect an silverlight apps background task with azure? Because I get issues with the assemblies when adding the nuget package for the backgroundtask. – JTIM May 19 '16 at 13:41

1 Answers1

0

Your BackgroundTask is asynchronous and you are not obtaining a BackgroundTaskDeferral, thus your task ends when calling await. As it is said at MSDN:

  1. If you run any asynchronous code in your background task, then your background task needs to use a deferral. If you don't use a deferral, then the background task process can terminate unexpectedly if the Run method completes before your asynchronous method call has completed.

It should help:

BackgroundTaskDeferral _deferral;

public async void Run(IBackgroundTaskInstance taskInstance)
{
    _deferral = taskInstance.GetDeferral();
    // your async code
    _deferral.Complete();
}

You should also be aware about the limits of your BackgroundTask and especially when connecting to internet - see MSDN.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Hi Romasz, I'v use this code in your project and It's correctly working but I have a Universal App, solution name is: MyApplication. I'v created by your instruction a MyTask project but when I fire background task, my application terminates. I'v added MyTaskProject to other project and I Set MyTask.FirstTask for backgroundTask entry point. where I wrong? – WindowsPhoneDev Mar 29 '15 at 09:16
  • @WindowsPhoneDev Check declarations in manifest file, check if BackgroundProject is Windows Runtime Component, check names and references. And from what I see, you were able to run the task (your question), then what have changed? – Romasz Mar 29 '15 at 09:20
  • Thanks Romasz, In my question I'v used your project not mine. I created a project with this name: MyApplication.MyTask. I set Entry point to MyApplication.MyTask.FirstTask and It's correctly works. ||| another question: in your code, when I register a background task, If i press the Register button, that your loop didn't work and it's create a new backgroundtask. It's a problem or not? thank you so much – WindowsPhoneDev Mar 29 '15 at 09:42
  • @WindowsPhoneDev I'm not quite sure what you are asking - if Btask is not registered, then it's up to you to do something with this. Recognize the case and perform suitable steps depending on your needs. Also remember to check if BTask is not already registered and unregister if not needed anymore. Please also don't ask too many questions in comments - it doesn't suppose to work like that. – Romasz Mar 29 '15 at 10:03
  • Looking at your output window screen shot, You background task is registered. You only have to make sure that a deferral is defined and you are good to go. – golldy Mar 30 '15 at 04:57