我试图限制在vb.net中打开webbrowser的时间;经过大量搜索后的结论似乎是线程!我试过下面的代码;这包括我试过的两种方法,但没有任何效果; 我试图在另一个线程中在10秒内打开网站,如果网站没有回复,那么去下一个网站并丢弃那个;我的代码如下: 第一次尝试:
Option Explicit On
Option Strict On
Imports System.Threading
Public Class Form1
Dim WorkerThread As Thread
Dim StopThread As Boolean = True
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(" ************************** This is the MAIN thread ************************** ")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show(" ************************** This is the SECOND thread ************************** ")
If StopThread = False Then
StopThread = True
Else
StopThread = False
WorkerThread = New Thread(AddressOf Navigate)
WorkerThread.IsBackground = True
WorkerThread.Start()
End If
End Sub
Sub Navigate()
WebBrowser1.Navigate("http://www.mekdam.com")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
End Sub
End Class
第二次审判
Option Explicit On
Option Strict On
Imports System.Threading
Public Class Form1
Dim WorkerThread As Thread
Dim StopThread As Boolean = True
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(" ************************** This is the MAIN thread ************************** ")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show(" ************************** This is the SECOND thread ************************** ")
If StopThread = False Then
StopThread = True
Else
StopThread = False
WorkerThread = New Thread(AddressOf Navigate)
WorkerThread.IsBackground = True
WorkerThread.Start()
End If
End Sub
Sub Navigate()
WebBrowser1.Navigate("http://www.mekdam.com")
While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
End Sub
End Class