Results 1 to 20 of 20

Thread: [2008] Cross Thread issuses with WebBrowser control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    [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

  2. #2
    Lively Member
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    86

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  4. #4
    Lively Member
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    86

    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()

  5. #5

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  8. #8

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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

  10. #10

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  12. #12

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.
    Last edited by DC123; Jun 8th, 2008 at 12:13 PM.

  14. #14

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  16. #16
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Cross Thread issuses with WebBrowser control

    "Specified cast is not valid" isnt a cross-thread issue. On what line does it occur?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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?

  19. #19
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. 'This is the method that runs in a separate thread
    2.     Private Sub doSomething()
    3.         Dim innerHTML As String = GetInnerHTML()
    4.         MessageBox.Show(innerHTML)
    5.     End Sub
    6.  
    7.     Private Delegate Function GetInnerHTMLCallBack() As String
    8.     Private Function GetInnerHTML() As String
    9.         If WebBrowser1.InvokeRequired Then
    10.             Return CStr(WebBrowser1.Invoke(New GetInnerHTMLCallBack(AddressOf GetInnerHTML)))
    11.         Else
    12.             Return WebBrowser1.Document.Body.InnerHtml
    13.         End If
    14.     End Function
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    81

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width