2016-12-06 57 views
-1

我想寫一個簡單的程序,如果某個進程正在運行,並且如果是的話每5秒會檢查一次,然後殺死它。程序應該在後臺運行,並且每次啓動機器時都會啓動。 其用VB編寫的 進程迄今:掃描進程,然後殺死它一旦找到並重新開始

Module Module1 
Private Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean 
Private Declare Auto Function GetConsoleWindow Lib "kernel32.dll"() As IntPtr 
Private Const SW_HIDE As Integer = 0 

Sub Main() 
eh: 
    Dim hWndConsole As IntPtr 
    hWndConsole = GetConsoleWindow() 
    ShowWindow(hWndConsole, SW_HIDE) 
    For Each proc As Process In Process.GetProcessesByName("hl") 'hl is the process to look for 
     proc.WaitForExit(5000) 'wait up to 5 seconds. 
     proc.Kill() 'force the process to exit. 

    Next proc 
    GoTo eh 
    Threading.Thread.Sleep(5000) 'Sleep for 5 sec and start over 
End Sub 
End Module 

但問題是,它顯示控制檯窗口每次它開始,它也崩潰,它殺死了檢測過程

回答

-1

當然以後,因爲你嘗試搜索你已經殺了一個過程:For Each proc As Process In Process.GetProcessesByName("hl")

嘗試類似的東西:

For Each prog As Process In Process.GetProcesses 
    If prog.ProcessName = "hl" Then 
      prog.Kill() 
      Exit For 
    End If 
Next 
+0

謝謝喲你的迴應。我按照你的建議改變了它,但是它在殺死進程之後仍然崩潰,並且該進程應該由用戶重新啓動。 – degaro

+0

所以..首先使用'Try Catch',然後你可以更好地處理錯誤。我建議你在'單獨的線程'中運行這段代碼。 使用Try編輯代碼後,讓我知道錯誤消息。 – Tyler