WPF TPL WebBrowser 控件错误:调用线程必须是 STA

发布于 2024-11-29 01:03:19 字数 1462 浏览 0 评论 0原文

我有以下问题: 我想在后台下载一个带有 WebBrowser Control (WPF) 的网站,然后解析该网站。下载应该处于“紧密循环”中。我不需要显示该页面。我只需要 HTML 源作为字符串。问题是网站在加载后会用 JavaScript 动态添加内容,这就是我使用这种方法的原因。 这实际上应该同时适用于多个站点。

在我的以下代码中存在错误:“调用线程必须是 STA,因为许多 UI 组件需要此”。

我知道如果我将 TaskScheduler 更改为当前上下文,我不会收到错误消息。

但是如何在后台下载 HTML 源代码呢?

添加 我必须将它添加到例如 StackPanel 中,如果没有此添加,它不起作用

stackpanel.Children.Add(wb2);

有什么想法吗?

预先感谢您

private void Button1_Click(object sender, RoutedEventArgs e)
        {Task.Factory.StartNew(
                    () =>
                    {
                        WebBrowser wb2 = new WebBrowser();  // That's where I get the error essage
                        wb2.Source = new Uri(MySite);
                        wb2.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);

                    }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
        }

void wb_LoadCompleted(object sender, NavigationEventArgs e)
        {
            WebBrowser w = sender as WebBrowser;
            HtmlDocument document = new HtmlDocument(w.Document);

更新

我用StaTaskScheduler尝试了这样的操作,但没有得到UI更新任何我的错误所在的deas并将其发布在一个额外的线程中

StaskScheduler(TPL 扩展)和WebBrowser 控件 WPF - 错误的线程

谢谢!

I've got the following problem:
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 needn't display the page. I only need the HTML source as a string. The problem is that the site dynamically adds content with JavaScript after it's loaded, that's why I use this approach.
This should actually work with several sites simultaneously.

In my following Code there's the error:"The calling thread must be STA, because many UI components require this".

I know if I change the TaskScheduler to the current context I get no error message.

But how can I download the HTML Source in the background?

ADDITION
I have to add it to a e.g. StackPanel withoud this addition it doesn't work

stackpanel.Children.Add(wb2);

Any ideas?

Thank you in advance

private void Button1_Click(object sender, RoutedEventArgs e)
        {Task.Factory.StartNew(
                    () =>
                    {
                        WebBrowser wb2 = new WebBrowser();  // That's where I get the error essage
                        wb2.Source = new Uri(MySite);
                        wb2.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);

                    }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
        }

void wb_LoadCompleted(object sender, NavigationEventArgs e)
        {
            WebBrowser w = sender as WebBrowser;
            HtmlDocument document = new HtmlDocument(w.Document);

UPDATE

I tried it like this with the StaTaskScheduler nonetheless get no UI update Any deas where my mistake is and postes it in an extra thread

StaTaskScheduler (TPL Extension) and WebBrowser Control WPF - Wrong Thread

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-12-06 01:03:19

WebBrowser 已经是异步的,尽管我相信它会在 UI 线程上执行脚本。

如果您想将其拆分为真正独立的线程,可以使用 STAThreadScheduler。听起来您希望每个网站有一个(STA)线程。

The WebBrowser is already asynchronous, though I believe it would execute scripts on the UI thread.

If you want to split it off into a truly separate thread, you can use STAThreadScheduler. It sounds like you'd want one (STA) thread per web site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文