Access to Internet in Background

2019-08-09 00:18发布

问题:

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.

回答1:

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.