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!