0

Hi i have a problem to detect if there any error in webbrowser - First i used this code on webbrowser control (webbrowser1, webbrowser2 ..) :

private sub str
 WebBrowser2.Navigate(TextBox5.Text)
 Dim axBrowser As SHDocVw.WebBrowser
 DirectCast(WebBrowser2.ActiveXInstance, SHDocVw.WebBrowser)

 AddHandler axBrowser.NavigateError, AddressOf axBrowser_NavigateError
end sub
 Private Sub axBrowser_NavigateError(ByVal pDisp As Object, ByRef URL As Object, ByRef Frame As Object, ByRef StatusCode As Object, ByRef Cancel As Boolean)

      MessageBox.Show("Error")

 End Sub

it working perfect but when i use a newwebbrowser added into FlowLayoutPanel to use multitheard it not working i can't detect if there any error navigate where is the problem :

Dim myWBrowser As New WebBrowser
FlowLayoutPanel2.Controls.Add(myWBrowser)
myWBrowser.Navigate(uri)
 Dim s3 As New Stopwatch
     s3.Start()
     Do
         Application.DoEvents()
    Loop Until myWBrowser.ReadyState = WebBrowserReadyState.Complete
    s3.Stop()
 Dim axBrowser As SHDocVw.WebBrowser = DirectCast(myWBrowser.ActiveXInstance, SHDocVw.WebBrowser)

 AddHandler axBrowser.NavigateError, AddressOf axBrowser_NavigateError

thanks

  • 1
    Don't run a WebBrowser from Thread other than the UI Thread. It won't work. Or, it could, doing something like this: [WebBrowser Control in a new thread](https://stackoverflow.com/a/4271581/7444103). Do you need to? The WebBrowser class is event driven, it won't block the main thread anyway. Maybe you could perform some processing running Tasks, if required. If you need to show the WebBrowser control interface in the main UI Thread, threads are pretty much useless. – Jimi Feb 18 '20 at 05:00
  • @Jimi thank you for response i tried the answer but there a problem that i cant called the function navigate-error directly this problem it only with this function : Dim axBrowser As SHDocVw.WebBrowser = DirectCast(myWBrowser.ActiveXInstance, SHDocVw.WebBrowser) AddHandler axBrowser.NavigateError, AddressOf axBrowser_NavigateError – Boubekri Abderrahmane Feb 18 '20 at 21:02
  • Don't use threads. – Jimi Feb 18 '20 at 21:03
  • @Jimi but i need to use multitheards im under create a bot – Boubekri Abderrahmane Feb 18 '20 at 21:07
  • @Jimi are there any astuce to know if the page display a error or not ? – Boubekri Abderrahmane Feb 18 '20 at 21:08
  • What you're doing now it's ok, it's the WebBrowser control that doesn't work well with multi-threading. You don't need threading here. Also, you're adding multiple WB cotrols to a FlowLayoutPanel. So you have a single UI Thread, otherwise you'd have the same problem with the FlowLayoutPanel. To make any of the WinForms control to work on a Thread, the Thread needs to be a `STAThread` and you cannot move controls from one thread to another or rise events in another Thread directly. – Jimi Feb 18 '20 at 21:23
  • @Jimi i dont have a single UI Thread i create 100 theards and all function in webbrowser working with all theard each WB added in flowlayoutpanel have a new control like time stay in page and go to other link .... only this function to detect if there any error in page that not will working it ignoret and pass to next function – Boubekri Abderrahmane Feb 18 '20 at 22:44

0 Answers0