十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
軟糖來回答羅:通過System.Diagnostics命名空間下的Process類來關(guān)閉程序的進(jìn)程
成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,先為成華等服務(wù)建站,成華等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為成華企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
Dim?進(jìn)程集合?=?Process.GetProcessesByName("進(jìn)程名稱")
For?Each?進(jìn)程?In?進(jìn)程集合
進(jìn)程.Kill()
'進(jìn)程.Close()?'或者使用關(guān)閉
Next
也可以先獲取所有進(jìn)程,再來判斷這些進(jìn)程的名稱ProcessName
Dim?獲取本地所有進(jìn)程?=?Process.GetProcesses()
For?Each?進(jìn)程?In?獲取本地所有進(jìn)程
If?進(jìn)程.ProcessName?=?"explorer.exe"?Then?進(jìn)程.Kill()
Next
vb.net中如何結(jié)束一個(gè)線程
一般而言,如果您想終止一個(gè)線程,您可以使用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())
當(dāng)然,在調(diào)用Abort方法后,線程并不是立刻終止,要等線程的所有finally快中的代碼完成后才會(huì)完全終止. 所以在主線程中可以用Join方法來同步,當(dāng)線程還未完全終止時(shí),t.Join()將處于等待,直到t線程完全結(jié)束后再繼續(xù)執(zhí)行后面的語句。
Abort方法是會(huì)導(dǎo)致線程跳出一個(gè)異常錯(cuò)誤的,你需要在代碼中捕獲該異常。下面是一個(gè)比較完整的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()方法用來永久銷毀一個(gè)線程,而且將拋出ThreadAbortException異常。使終結(jié)的線程可以捕獲到異常但是很難控制恢復(fù),僅有的辦法是調(diào)用Thread.ResetAbort()來取消剛才的調(diào)用,而且只有當(dāng)這個(gè)異常是由于被調(diào)用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調(diào)用Thread.ResetAbort()來取消Thread.Abort()操作。
Sub aaa()
MsgBox("111")
application.exit
End Sub
詳細(xì)查看了你的問題,完全不需要使用線程sleep的方法。
在這邊,我要給你引入一個(gè)概念。就是Form窗體的兩個(gè)方法。
Form.Show()和Form.ShowDialog()
第一個(gè)Show方法運(yùn)行后,你的主程序還是會(huì)進(jìn)行,你的代碼還是會(huì)繼續(xù)進(jìn)行下去。此時(shí),窗體還是彈出的。
第二個(gè)ShowDialog方法運(yùn)行后,此時(shí),彈出的一個(gè)窗體,你不將其關(guān)閉,那么,主程序就會(huì)一直等待這個(gè)窗體關(guān)閉后,才執(zhí)行后面的代碼。
所以,你只需要使用showdialog這個(gè)方法,即可達(dá)到你想要的結(jié)果。
那么,如何才能讓彈出的窗體不改變呢?很簡單
你可以找到窗體的formcloseing事件,在這個(gè)事件中,有一個(gè)參數(shù):e
這個(gè)e有一個(gè)屬性,就是e.handle,表示是否處理過這個(gè)事件了。
這個(gè)時(shí)候,如果你不想讓窗體關(guān)閉,你可以寫:
e.handle=true,那么表示已經(jīng)處理過窗體關(guān)閉的事件了。
注:有些情況,可能是:e.cancel=true