Reputation: 1855
I'm trying to implement simple webpage screen capture program. When I was using my code on UI thread all seemed to work with no problems, but when I called method from non-UI threads I couldn't get WebBrowserDocumentCompleted event to fire any more. I also tried this:
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += WebBrowserDocumentCompleted;
wb.Visible = true;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
}
Program just hangs in while loop, indefinitely. Any ideas?
Upvotes: 4
Views: 14450
Reputation: 117
The solution is unbelievable. After hours of searching and trying things, I found the answer.
In the properties of webBrowser1
, I set the AllowNavigation = false
.
If set to false
, it only registers the DocumentCompleted
event only ONCE, but when the AllowNavigation = true
(which it is by default), the DocumentCompleted
event is fired multiple times.
Upvotes: 1
Reputation: 179
In your build output window, look for the below warning:
"Found conflicts between different versions of the same dependent assembly"
If you find this warning, ensure all assemblies use the same version of the conflicting Referenced assembly and recheck if WebBrowser delivers your events.
Upvotes: 0
Reputation: 4554
Something like this:
public void Start()
{
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();
wb.DocumentCompleted += WebBrowserDocumentCompleted;
wb.Visible = true;
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(url);
}
private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if( (sender as WebBrowser).ReadyState == System.Windows.Forms.WebBrowserReadyState.Complete)
{
// Do what ever you want to do here when page is completely loaded.
}
}
I hope this helps you in your quest.
Upvotes: 6