|
-
Mar 6th, 2011, 02:36 PM
#1
Thread Starter
Fanatic Member
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:
Public Shared Sub putHtmlIntoWebBrowser(ByVal html As String)
'WebBrowser.DocumentText = html
WebBrowser.Document.OpenNew(True)
Dim noscriptHtml = removeScript(html)
appendToTextFile(noscriptHtml + vbNewLine, "laststring.html", Scripting.IOMode.ForWriting)
WebBrowser.ScriptErrorsSuppressed = True
WebBrowser.Document.Write(noscriptHtml)
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.
-
Mar 6th, 2011, 06:09 PM
#2
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.
-
Mar 6th, 2011, 07:51 PM
#3
Re: Accessing Webbrowser from Another Thread
Try like this:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click WebBrowser1.DocumentText = "<h1>From UI thread ....</h1>" Dim th As New Threading.Thread(AddressOf WorkerThread) th.Start() End Sub Private Sub WorkerThread() Dim wb As WebBrowser = WebBrowserX() '' now you have a reference to the webbrowser in your worker thread. Do whatever you want with this. wb.DocumentText = "<h1>From worker thread...</h1>" End Sub Private Delegate Function WebBrowserDelegate() As WebBrowser Private Function WebBrowserX() As WebBrowser If WebBrowser1.InvokeRequired Then Return DirectCast(Me.WebBrowser1.Invoke(New WebBrowserDelegate(AddressOf WebBrowserX)), WebBrowser) Else Return Me.WebBrowser1 End If End Function
-
Mar 6th, 2011, 08:33 PM
#4
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.
-
Mar 6th, 2011, 09:37 PM
#5
Thread Starter
Fanatic Member
Re: Accessing Webbrowser from Another Thread
I am reading this http://www.vbforums.com/showthread.php?t=544824 Your codebank is in vbforums?
-
Mar 6th, 2011, 09:44 PM
#6
Re: Accessing Webbrowser from Another Thread
 Originally Posted by teguh123
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.
-
Mar 7th, 2011, 02:06 AM
#7
Thread Starter
Fanatic Member
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
-
Mar 7th, 2011, 02:53 AM
#8
Banned
Re: Accessing Webbrowser from Another Thread
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
|