|
-
Jun 8th, 2008, 09:07 AM
#1
Thread Starter
Lively Member
[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
-
Jun 8th, 2008, 09:13 AM
#2
Lively Member
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
Last edited by Deathader; Jun 8th, 2008 at 09:32 AM.
-
Jun 8th, 2008, 09:27 AM
#3
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
-
Jun 8th, 2008, 09:30 AM
#4
Lively Member
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()
-
Jun 8th, 2008, 09:40 AM
#5
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.
-
Jun 8th, 2008, 11:06 AM
#6
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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
-
Jun 8th, 2008, 11:07 AM
#7
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
-
Jun 8th, 2008, 11:13 AM
#8
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.
-
Jun 8th, 2008, 11:25 AM
#9
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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
-
Jun 8th, 2008, 11:34 AM
#10
Re: [2008] Cross Thread issuses with WebBrowser control
-
Jun 8th, 2008, 11:45 AM
#11
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 Originally Posted by Deepak Sakpal
What problem?
"Specified cast is not valid"
My original question has the details.
-
Jun 8th, 2008, 11:55 AM
#12
Re: [2008] Cross Thread issuses with WebBrowser control
Is your web page accessible to out side world?
-
Jun 8th, 2008, 11:58 AM
#13
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
Last edited by DC123; Jun 8th, 2008 at 12:13 PM.
-
Jun 8th, 2008, 12:17 PM
#14
Re: [2008] Cross Thread issuses with WebBrowser control
and what is that www address?
-
Jun 8th, 2008, 12:22 PM
#15
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
-
Jun 8th, 2008, 01:01 PM
#16
Re: [2008] Cross Thread issuses with WebBrowser control
"Specified cast is not valid" isnt a cross-thread issue. On what line does it occur?
-
Jun 8th, 2008, 02:00 PM
#17
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
-
Jun 8th, 2008, 02:17 PM
#18
Thread Starter
Lively Member
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?
-
Jun 8th, 2008, 02:22 PM
#19
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
-
Jun 9th, 2008, 04:59 AM
#20
Thread Starter
Lively Member
Re: [2008] Cross Thread issuses with WebBrowser control
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|