在 VB .net 中线程化多个 WebBrowser
我正在选项卡内构建多个网络浏览器(每个选项卡 1 个预定义网络浏览器控件),并且我希望它们全部同时加载,否则必须在线程中运行。不幸的是,我认为错误消息中提供了一个有效的事实,即这是不可能的。请帮助我检查下面的简单程序代码及其错误,以防我尝试释放 2 个 Web 浏览器控件,但当我将其留给单个 Web 浏览器控件时,它工作正常。有什么解决办法吗?
Imports System.Threading
Public Class Form1
Dim mythread1 As Thread
Dim mythread2 As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mythread1 = New Thread(AddressOf mysub1)
mythread2 = New Thread(AddressOf mysub2)
mythread1.IsBackground = True
mythread2.IsBackground = True
mythread1.Start()
mythread2.Start()
End Sub
Public Sub mysub1()
Dim HTML As String
WebBrowser1.Navigate("about:blank")
mythread1.Abort()
End Sub
Public Sub mysub2()
WebBrowser2.Navigate("about:blank")
mythread2.Abort()
End Sub
I'm building a multiple webbrowser inside tabs( 1 predefine webbrowser control per tab) and I want them all to load at the same time or other words must run in thread. Unfortunately I feel a valid fact came from the error message that this is something not possible. Pls help me to check my simple program code below and its error in case i tried releasing 2 webbrowser controls but when I leave it to single web browser control it works fine. Any workaround?
Imports System.Threading
Public Class Form1
Dim mythread1 As Thread
Dim mythread2 As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mythread1 = New Thread(AddressOf mysub1)
mythread2 = New Thread(AddressOf mysub2)
mythread1.IsBackground = True
mythread2.IsBackground = True
mythread1.Start()
mythread2.Start()
End Sub
Public Sub mysub1()
Dim HTML As String
WebBrowser1.Navigate("about:blank")
mythread1.Abort()
End Sub
Public Sub mysub2()
WebBrowser2.Navigate("about:blank")
mythread2.Abort()
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下 msdn 文章:“控件上有四种可以线程安全调用的方法:Invoke、BeginInvoke、EndInvoke 和 CreateGraphics 以及 InvokeRequired 属性”
无法直接调用 WebBrowser1.Navigate 从另一个线程
您 ://msdn.microsoft.com/en-us/library/ms171728.aspx" rel="nofollow noreferrer">如何:对 Windows 窗体控件进行线程安全调用
Following msdn article: "There are four methods on a control that are thread safe to call: Invoke, BeginInvoke, EndInvoke and CreateGraphics and InvokeRequired property"
You cannot make a direct call to WebBrowser1.Navigate from another thread
Follow How to: Make Thread-Safe Calls to Windows Forms Controls
默认情况下,网络浏览器是异步的,因此您不需要对它们进行线程处理,因为它们将按顺序运行。
如果它可以加快你的速度,那么也许可以切换到 webclient 或 httpwebrequest。您无需下载图像即可运行,并且可以对它们进行多线程处理。
Webbrowsers are by default asynchronous so you don't need to thread them as they will each run in order.
If it's speed your after then maybe switch up to webclient or httpwebrequest. You don't need to download the images to run and can multi-thread those.