StaTaskScheduler(TPL扩展)和WebBrowser控件WPF-错误的线程 [英] StaTaskScheduler (TPL Extension) and WebBrowser Control WPF - Wrong Thread

查看:78
本文介绍了StaTaskScheduler(TPL扩展)和WebBrowser控件WPF-错误的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台下载带有WebBrowser控件(WPF)的网站,然后解析该网站.下载应该在紧密循环"中.我只需要将源作为字符串,就用下面的代码尝试了一下,但并没有得到结果. 如果我不将它与StaTaskScheduler一起使用,则该程序似乎在循环期间冻结. 有任何想法吗?

I want to download a web site with the WebBrowser Control (WPF) in the Background and parse the site afterwards. The download should be in a "tight loop". I only need the source as a string. I tried it with the following code which didn't give me the results. If I don't use it with StaTaskScheduler the program seems to freez during the loop. Any Ideas ?

谢谢

StaTaskScheduler sta = new StaTaskScheduler(numberOfThreads: 1); 
private void Button1_Click(object sender, RoutedEventArgs e)
        { 
   for (int i = 0; i < 2; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    WebBrowser wb3 = new WebBrowser();
                    wb3.Source = new Uri("MyURL");
                    n++;
                    wb3.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
                }, CancellationToken.None, TaskCreationOptions.None, sta);
            }
        }

void wb_LoadCompleted(object sender, NavigationEventArgs e)
    {

 WebBrowser w = sender as WebBrowser;
  HtmlDocument document = new HtmlDocument(w.Document);

blockingCollection.Add(document.Body.OuterHtml);

        Task.Factory.StartNew(
           () =>
           {
               while (!blockingCollection.IsCompleted)
               {
                   string dlcode;
                   Thread.Sleep(500);
                   if (blockingCollection.TryTake(out dlcode))
                   {
    // tb is a TextBox
                         Dispatcher.BeginInvoke(new Action(() => { tb.Text = dlcode; }));
                   }
               }
           }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); 

}

推荐答案

我建议不要为此使用WebBrowser,而应直接使用WebClient.这是通过使例程将下载数据包装在Task中来最简单地完成的:

I would recommend not using the WebBrowser for this, but rather just use a WebClient directly. This is easiest done by making a routine to wrap the download data in a Task:

Task<string> DownloadStringAsync(Uri address)
{
     TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
     WebClient client = new WebClient();

     // Note that you can add error checking here by looking at e.Error/etc, and setting the cancel/error in tcs appropriately...
     client.DownloadStringCompleted += (o,e) => tcs.SetResult(e.Result);
     client.DownloadStringAsync(address);

     return tcs.Task;
}

有了这个,您应该能够直接使用这些任务,并在完成时将其结果设置到BC中.这比尝试旋转用于视觉用途的WebBrowser控件要简单得多.

With this, you should be able to just use these tasks directly, and set their results into the BC on completion. This would be far simpler than trying to spin of a WebBrowser control, which is intended for visual use.

这篇关于StaTaskScheduler(TPL扩展)和WebBrowser控件WPF-错误的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆