在后台访问互联网

Access to Internet in Background

我正在使用 C# 和 Windows Phone 8.1 作为通用应用程序。

我在这篇文章中使用了后台任务:http://www.romasz.net/how-to-add-a-backgroundtask/

我想当我的应用程序在后台时,打开一个站点并获取 html 源代码。我使用过这段代码(在后台项目中):

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));
    }

这是我的后台调试输出:

'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).

它被解雇了,但无法访问我的网站。请帮助我。

谢谢。

您的 BackgroundTask 异步的 而您没有获得 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.

应该有帮助:

BackgroundTaskDeferral _deferral;

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

您还应该了解 BackgroundTask 的限制,尤其是在连接到互联网时 - see MSDN.