访问互联网背景(Access to Internet in Background)

2019-10-22 09:56发布

我使用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).

它的发射,但不能访问我的网站。 请帮我。

谢谢。

Answer 1:

BackgroundTask异步的 ,你是不是获得BackgroundTaskDeferral ,因此要调用的await时,你的任务结束。 由于它是在说MSDN :

  1. 如果你在你的后台任务运行任何异步代码,那么你的后台任务需要使用延期。 如果不使用延期,那么后台任务过程中可能会意外,如果你的异步方法调用完成之前的运行方法完成终止。

它应该帮助:

BackgroundTaskDeferral _deferral;

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

你也应该知道你的BackgroundTask的限制,特别是连接到互联网时- 请参阅MSDN 。



文章来源: Access to Internet in Background