Public?Sub?KillProcessFromMemorry()
10年積累的成都做網(wǎng)站、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有二道免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
'注意進(jìn)程名稱的大小寫
Dim?procName?As?String?=?"EXCEL"
For?Each?proc?In?Process.GetProcesses
If?proc.ProcessName?Like?procName?Then
Console.WriteLine("'{0}'?is?running,?do?you?want?to?quit?(y,?n)",?procName)
If?Console.ReadKey.Key?=?ConsoleKey.Y?Then
proc.Kill()
End?If
End?If
Next
End?Sub
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方法來(lái)同步,當(dāng)線程還未完全終止時(shí),t.Join()將處于等待,直到t線程完全結(jié)束后再繼續(xù)執(zhí)行后面的語(yǔ)句。
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()方法用來(lái)永久銷毀一個(gè)線程,而且將拋出ThreadAbortException異常。使終結(jié)的線程可以捕獲到異常但是很難控制恢復(fù),僅有的辦法是調(diào)用Thread.ResetAbort()來(lái)取消剛才的調(diào)用,而且只有當(dāng)這個(gè)異常是由于被調(diào)用線程引起的異常。因此,A線程可以正確的使用Thread.Abort()方法作用于B線程,但是B線程卻不能調(diào)用Thread.ResetAbort()來(lái)取消Thread.Abort()操作。
好像不難吧?
我放進(jìn)了Button1的Click事件里。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo Errmessages '在做系統(tǒng)操作時(shí)加排錯(cuò)標(biāo)簽是個(gè)好習(xí)慣
Dim TargetName As String = "ibmdict" '存儲(chǔ)進(jìn)程名為文本型,注:進(jìn)程名不加擴(kuò)展名
Dim TargetKill() As Process = Process.GetProcessesByName(TargetName) '從進(jìn)程名獲取進(jìn)程
Dim TargetPath As String '存儲(chǔ)進(jìn)程路徑為文本型
If TargetKill.Length 1 Then '判斷進(jìn)程名的數(shù)量,如果同名進(jìn)程數(shù)量在2個(gè)以上,用For循環(huán)關(guān)閉進(jìn)程。
For i = 0 To TargetKill.Length - 1
TargetPath = TargetKill(i).MainModule.FileName
TargetKill(i).Kill()
Next
ElseIf TargetKill.Length = 0 Then '判斷進(jìn)程名的數(shù)量,沒(méi)有發(fā)現(xiàn)進(jìn)程直接彈窗。不需要的,可直接刪掉該If子句
MsgBox("沒(méi)有發(fā)現(xiàn)進(jìn)程!")
Exit Sub
ElseIf TargetKill.Length = 1 Then '判斷進(jìn)程名的數(shù)量,如果只有一個(gè),就不用For循環(huán)
TargetKill(0).Kill()
End If
MsgBox("已終止" TargetKill.Length "個(gè)進(jìn)程") '彈窗提示已終止多少個(gè)進(jìn)程
Errmessages: ‘定義排錯(cuò)標(biāo)簽
If Err.Description Nothing Then ’判斷有無(wú)錯(cuò)誤,如果有,則 ↓
MsgBox(Err.Description) '當(dāng)出現(xiàn)錯(cuò)誤時(shí),彈窗提示
End If
End Sub
可根據(jù)需要自行修改,這個(gè)備注夠完善了吧?不會(huì)的再Hi我。