WebBrowser.Navigated Only Fires when I MessageBox.Show ();
I have a WebBrowser control that is dynamically created from the background STA thread because the parent thread is the BackgroundWorker and has many other capabilities.
The problem is that the Navigated event never fires unless I find the MessageBox.Show () in the method that told it to .Navigate (). I will explain:
ThreadStart ts = new ThreadStart(GetLandingPageContent_ChildThread);
Thread t = new Thread(ts);
t.SetApartmentState(ApartmentState.STA);
t.Name = "Mailbox Processor";
t.Start();
protected void GetLandingPageContent_ChildThread()
{
WebBrowser wb = new WebBrowser();
wb.Navigated += new WebBrowserNavigatedEventHandler(wb_Navigated);
wb.Navigate(_url);
MessageBox.Show("W00t");
}
protected void wb_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender; // Breakpoint
HtmlDocument hDoc = wb.Document;
}
This works great; but the mailbox will get in the way as this is an automation app. When I remove MessageBox.Show () the WebBrowser.Navigated event never fires. I tried to preempt this line with Thread.Sleep () and suspend the parent thread.
Once I get this out of the way, I intend to pause the parent thread while the WebBrowser does its job and finds a way to pipe the resulting HTML back to the parent thread so that it can continue with further logic.
Why is he doing this? How can I fix this?
If someone can provide me with a way to get the content of a web page, fill in some data and return the content of the page on the other side of the submit button, all against a web server that does not support POST and passing data through QueryString, I also agree with this answer. since this whole exercise would be unnecessary.
Solution: I ended up not using the BackgroundWorker and slave thread at all as suggested by the team architect ... Though at the expense of responsiveness :(
source to share
WebBrowser
won't do much unless shown and has an associated UI thread; are you showing the form it is on? You need to use DOM etc. The form can be disabled if you don't want to display it to the user, but it won't work well in a service (for example).
For cleanup purposes, you can generally simulate a regular HTML browser with WebClient
etc. This is not enough? You can use tools like " Fiddler " to figure out the exact query you need to make on the server. Moreover, you can check out the HTML Agility Pack , which offers DOM access to HTML without a browser.
source to share
Navigated and DocumentComplete events will not fire if WebBrowser visibility is set to false. You can work around this limitation by making the WebBrowser visible, but setting its location to be outside of the UI, for example:
wb.Visible = true;
wb.Left = -wb.Width; // notice the minus sign
source to share
Is there a GUI thread already started? The WebBrowser object might be using a GUI thread to handle events. In this case, you should call Application.Run () from the thread that the WebBrowser creates (replace MessageBox.Show () with this). Application.Run () will hang until Application.Exit () is called.
Trying to check it out now.
source to share