可能原因:timer不是由當前線程創建的。
創新互聯建站專注為客戶提供全方位的互聯網綜合服務,包含不限于網站設計、做網站、北票網絡推廣、微信平臺小程序開發、北票網絡營銷、北票企業策劃、北票品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創新互聯建站為所有大學生創業者提供北票建站搭建服務,24小時服務熱線:028-86922220,官方網址:vcdvsql.cn
解決方案:在線程中實例化一個timer,然后觸發,可以去試一下;或者通過委托,調用timer。
先不用線程試一下吧。
感覺問題出在
Form2.Show() '啟動倒計時顯示窗口
這一句上。
這一句應該只需要執行一次就行了。
把它放在timer中,多次調用,造成假死。
使用api
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
用
t.Suspend() 這個是掛起線程。是一個異步方法。
t.Sleep 我看了是一個同步方法。要等這個方法運行完了才繼續下一步。
這和from.show和 from.showdiaglog區別差不多!
至于疑問2我也沒找到原因。
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線程完全結束后再繼續執行后面的語句。
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()操作。
名稱欄目:關于vb.net線程消除的信息
轉載源于:http://vcdvsql.cn/article30/hejgpo.html
成都網站建設公司_創新互聯,為您提供移動網站建設、動態網站、品牌網站建設、外貿建站、外貿網站建設、微信公眾號
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯