Sub Main()
創(chuàng)新互聯(lián)是一家專業(yè)提供香河企業(yè)網站建設,專注與成都網站建設、網站制作、H5技術、小程序制作等業(yè)務。10年已為香河眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
Dim thr As Thread
For Pi As Integer=0 To 4 //啟用5線程
MulParams =Pi vbTab sFile vbTab dFile vbTab 1 vbTab DelN vbTab cr vbTab cg vbTab cb vbTab IndexI
GlobalParamas(pi)=MulParams .Split(vbTab)
thr=New Thread(AddressOf MyMulThreadCaller)
thr.Start() //啟動多線程進程
Application.DoEvents
Next
End Sub
不可以,但是能夠在主線程的基礎上調用委托(Invoke)。(主線程會被占用)
例子:
Dim thr As Threading.Thread
Public Delegate Sub VoidDelegate()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
thr = New Threading.Thread(AddressOf Where)
thr.Start()
End Sub
Public Sub Where()
Me.BeginInvoke(New VoidDelegate(AddressOf WhereThr))
End Sub
Public Sub WhereThr()
Me.TextBox.text="0123456789"
End Sub
(Invoke)這種情況下,主線程被占用,所有由主線程執(zhí)行程序,都將被(wherethr函數(shù)執(zhí)行結束)后執(zhí)行。
你可以在timer前用if判斷網絡狀態(tài),
如斷開,可用
threading.Thread.Sleep(10000) ‘當前線程掛起10秒
’可以開一個新線程去讀取脫機數(shù)據(jù)。
如連接,則繼續(xù)執(zhí)行。
補充:
dim i as integer
'超過100次退出,避免死循環(huán)
for i=0 to 100
try
'ping你的端口
if ‘ok
exit for
else
threading.Thread.Sleep(10000) ‘當前線程掛起10秒
end
Catch ex As Exception
End Try
next
vb.net中如何結束一個線程
一般而言,如果您想終止一個線程,您可以使用System.Threading.Thread類的Abort方法. 例如:
Dim worker As ThreadStart = New ThreadStart(AddressOf workerthreadmethod)
Dim t As Thread = New Thread(worker)
t.Start()
MessageBox.Show("Wait for a while for the thread to start.")
MessageBox.Show(t.ThreadState.ToString())
t.Abort()
MessageBox.Show(t.ThreadState.ToString())
t.Join()
MessageBox.Show(t.ThreadState.ToString())
當然,在調用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會完全終止. 所以在主線程中可以用Join方法來同步,當線程還未完全終止時,t.Join()將處于等待,直到t線程完全結束后再繼續(xù)執(zhí)行后面的語句。
Abort方法是會導致線程跳出一個異常錯誤的,你需要在代碼中捕獲該異常。下面是一個比較完整的VB.NET線程例子:
Imports System
Imports System.Threading
Public Class MyTestApp
Public Shared Sub Main()
Dim t As New Thread(New ThreadStart(AddressOf MyThreadMethod))
'Start the thread
t.Start()
MsgBox("Are you ready to kill the thread?")
'Kill the child thread and this will cause the thread raise an exception
t.Abort()
' Wait for the thread to exit
t.Join()
MsgBox("The secondary thread has terminated.")
End Sub
Shared Sub MyThreadMethod()
Dim i As Integer
Try
Do While True
Thread.CurrentThread.Sleep(1000)
Console.WriteLine("This is the secondary thread running.")
Loop
Catch e As ThreadAbortException
MsgBox("This thread is going to be terminated by the Abort method in the Main function")
End Try
End Sub
End Class
Thread.Abort()方法用來永久銷毀一個線程,而且將拋出ThreadAbortException異常。使終結的線程可以捕獲到異常但是很難控制恢復,僅有的辦法是調用Thread.ResetAbort()來取消剛才的調用,而且只有當這個異常是由于被調用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調用Thread.ResetAbort()來取消Thread.Abort()操作。
Sub Main() Dim thr As New Thread(AddressOf 循環(huán)) thr.Start("a") End Sub Sub 循環(huán)(a() As String) '這里隨你干什么循環(huán)也行 For Each i As String In a MsgBox(i) Next End Sub
當前題目:vb.net線程 vbnet線程池
標題網址:http://muchs.cn/article4/docdeie.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、企業(yè)網站制作、網站改版、App設計、網站導航、定制網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)