Results 1 to 8 of 8

Thread: Accessing Webbrowser from Another Thread

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Accessing Webbrowser from Another Thread

    I am using webbrowser control to parse html.

    I used to have this code that works just fine

    vb.net Code:
    1. Public Shared Sub putHtmlIntoWebBrowser(ByVal html As String)
    2.             'WebBrowser.DocumentText = html
    3.             WebBrowser.Document.OpenNew(True)
    4.             Dim noscriptHtml = removeScript(html)
    5.             appendToTextFile(noscriptHtml + vbNewLine, "laststring.html", Scripting.IOMode.ForWriting)
    6.             WebBrowser.ScriptErrorsSuppressed = True
    7.             WebBrowser.Document.Write(noscriptHtml)
    8.  
    9.         End Sub

    After that I sort of manipulate the webbrowser control.

    Then I modify my program. Before the code is always run from the main UI thread. Now each thread can run the code (with synclock of course).

    Somehow it no longer works. webbrowser.document is simply not accessible.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Accessing Webbrowser from Another Thread

    The WebBrowser is a control and you cannot access controls except from the thread that created them. If your WebBrowser was created on the UI thread, as they almost always will be, then you can only access it from the UI thread.

    One option available to you may be to marshal a call to the UI thread to get the data, manipulate it on the secondary thread and then marshal back to the UI thread to update the control. Whether or not that's viable depends on what you're doing and how much UI interaction it requires. For more info on how to do that, follow the CodeBank link in my signature and check out my post on Accessing Controls From Worker Threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Accessing Webbrowser from Another Thread

    Try like this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     WebBrowser1.DocumentText = "<h1>From UI thread ....</h1>"
    3.  
    4.     Dim th As New Threading.Thread(AddressOf WorkerThread)
    5.     th.Start()
    6. End Sub
    7.  
    8. Private Sub WorkerThread()
    9.     Dim wb As WebBrowser = WebBrowserX()
    10.     '' now you have a reference to the webbrowser in your worker thread. Do whatever you want with this.
    11.     wb.DocumentText = "<h1>From worker thread...</h1>"
    12. End Sub
    13.  
    14. Private Delegate Function WebBrowserDelegate() As WebBrowser
    15. Private Function WebBrowserX() As WebBrowser
    16.     If WebBrowser1.InvokeRequired Then
    17.         Return DirectCast(Me.WebBrowser1.Invoke(New WebBrowserDelegate(AddressOf WebBrowserX)), WebBrowser)
    18.     Else
    19.         Return Me.WebBrowser1
    20.     End If
    21. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Accessing Webbrowser from Another Thread

    That's no use Pradeep, because you're still trying to access the WebBrowser on the secondary thread. Any access of the DocumentText property would have to be on the UI thread. You can certainly manipulate the String value on the secondary thread, but you must get that String from, or assign it to, the DocumentText property on the UI thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Accessing Webbrowser from Another Thread

    I am reading this http://www.vbforums.com/showthread.php?t=544824 Your codebank is in vbforums?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Accessing Webbrowser from Another Thread

    Quote Originally Posted by teguh123 View Post
    I am reading this http://www.vbforums.com/showthread.php?t=544824 Your codebank is in vbforums?
    That is the CodeBank thread I was referring to. The examples include getting and setting the Text of a TextBox. Getting and setting the DocumentText of a WebBrowser would be exactly the same.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Accessing Webbrowser from Another Thread

    I simply add this work.
    Public Shared Function successfulScanning(ByVal html As enchancedWinClient) As Boolean
    If WebBrowser.InvokeRequired Then
    WebBrowser.Invoke(Sub() successfulScanning = successfulScanning(html))
    Exit Function
    End If

  8. #8
    Banned
    Join Date
    Mar 2011
    Posts
    6

    Re: Accessing Webbrowser from Another Thread

    this helped me, thanks.

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