[2008] Cross Thread issuses with WebBrowser control
I am loading a web page into a WebBrowser control so that I can access it's HTML later in the code using WebBrowser1.DocumentText. My problem is that I can access the HTML from outside the BackgroundWorker1 thread but I can not access the HTML from within the thread. Instead I get the following error: "Specified cast is not valid" refering to the WebBrowser1.DocumentText line of code. I have checked that it is not a document loaded issue the document has loaded.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.google.com/")
BackgroundWorker1.RunWorkerAsync()
End Sub
Re: [2008] Cross Thread issuses with WebBrowser control
Try adding this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckForIllegalCrossThreadCalls = False
End Sub
it might work, might not ;)
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deathader
Try adding this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckForIllegalCrossThreadCalls = False
End Sub
it might work, might not ;)
It did not work, still getting the error.
Re: [2008] Cross Thread issuses with WebBrowser control
Ok, don't use backgroundworker. Just put the code into a normal sub, then excecute the sub
yoursub()
Re: [2008] Cross Thread issuses with WebBrowser control
Can you post the BackgroundWorker code from where you are trying to access the WebBrowser's text.
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deepak Sakpal
Can you post the BackgroundWorker code from where you are trying to access the WebBrowser's text.
Private Sub BackgroundWorkerMonitor_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerMonitor.DoWork
If Browser1NavMode = "HomePageLoaded" Then
WebBrowser1HTML = WebBrowser1.Document.Body.InnerHtml
End If
End Sub
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deathader
Ok, don't use backgroundworker. Just put the code into a normal sub, then excecute the sub
yoursub()
I want to keep the GUI functional.
Re: [2008] Cross Thread issuses with WebBrowser control
Why don't you use the DocumentCompleted Event of WebBrowser control? It occurs when the WebBrowser control finishes loading a document.
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deepak Sakpal
Why don't you use the DocumentCompleted Event of WebBrowser control? It occurs when the WebBrowser control finishes loading a document.
I tried this and it worked, but then I wanted to use the WebBrowser's Document Object Model (listed below) and the issue was again a problem.
Private Sub DoLogIn()
Dim args As Object() = {"en"}
WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "Password")
WebBrowser1.Document.GetElementById("code").SetAttribute("value", "Code")
WebBrowser1.Document.InvokeScript("doLogin", args)
End Sub
Re: [2008] Cross Thread issuses with WebBrowser control
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deepak Sakpal
What problem?
"Specified cast is not valid"
My original question has the details.
Re: [2008] Cross Thread issuses with WebBrowser control
Is your web page accessible to out side world?
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deepak Sakpal
Is your web page accessible to out side world?
No I am loading a website from the www into a WebBrowser control in a windows app.
Re: [2008] Cross Thread issuses with WebBrowser control
and what is that www address?
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Deepak Sakpal
and what is that www address?
I get the "Specified cast is not valid" error from any website I try even Google.
This is a cross threading issue on a windows application not a website issue.
Re: [2008] Cross Thread issuses with WebBrowser control
"Specified cast is not valid" isnt a cross-thread issue. On what line does it occur?
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Atheist
"Specified cast is not valid" isnt a cross-thread issue. On what line does it occur?
WebBrowser1HTML = WebBrowser1.Document.Body.InnerHtml
When I access the DOM via the above method from within the same thread all is OK but when I try to access the DOM from code in another thread I get the "Specified cast is not valid" error.
Re: [2008] Cross Thread issuses with WebBrowser control
When I access the GUI controls across threads I use Delegates. Is there a way of using a Delegate to access the WebBrowser control across a thread?
Re: [2008] Cross Thread issuses with WebBrowser control
Here's an example on how to use safe cross-threading to retrieve the property value of a control:
VB.NET Code:
'This is the method that runs in a separate thread
Private Sub doSomething()
Dim innerHTML As String = GetInnerHTML()
MessageBox.Show(innerHTML)
End Sub
Private Delegate Function GetInnerHTMLCallBack() As String
Private Function GetInnerHTML() As String
If WebBrowser1.InvokeRequired Then
Return CStr(WebBrowser1.Invoke(New GetInnerHTMLCallBack(AddressOf GetInnerHTML)))
Else
Return WebBrowser1.Document.Body.InnerHtml
End If
End Function
Re: [2008] Cross Thread issuses with WebBrowser control
Quote:
Originally Posted by Atheist
Here's an example on how to use safe cross-threading to retrieve the property value of a control:
Thanks that has fixed it.