0
votes

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!

1
I don't want to give an "answer" because this is not my area. Maybe you need to Dispatch your LoadedCompleted content. I never used Task or WebBrowser so its just a guess :) Another way to load something in the Background, which is also the WPF way, is to use a BackgroundWorker.dowhilefor
Thank you for your answer. The TPL in this case should replace the Backgroundworker, etc. I get the Error Message at WebBrowser wb2 = new WebBrowser();user774326

1 Answers

0
votes

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.