我完全是VB.net的新手,現在我正在開發Windows服務,它將在啓動時啓動* .exe。如果可執行文件被其他程序殺死,我該如何檢測並重新啓動這個可執行文件的進程?如何在VB.net中重新運行可執行文件
我的代碼如下:
Public Class MyWinService
Dim RetVal
Protected Overrides Sub OnStart(ByVal args() As String)
EventLog.WriteEntry("MyService Started")
RetVal = Shell("JobService.exe", 1)
End Sub
Protected Overrides Sub OnStop()
EventLog.WriteEntry("MyService Stopped")
Dim myProcess = Process.GetProcessById(RetVal)
myProcess.Kill()
End Sub
Protected Overrides Sub OnPause()
EventLog.WriteEntry("MyService Paused")
End Sub
Protected Overrides Sub OnContinue()
EventLog.WriteEntry("MyService Resumed")
End Sub
Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
If command = 200 Then
EventLog.WriteEntry("Custom Command 200 invoked")
ElseIf command = 210 Then
EventLog.WriteEntry("Custom Command 210 invoked")
End If
End Sub
Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
末級
非常感謝提前!
爲什麼你的服務啓動外部可執行文件,而不是隻是在做裏面的服務工作? – CoderDennis
可執行文件是用C++編寫的,似乎用C++設計一個windows服務要困難得多,所以我切換到VB.net並使用這個windows服務來運行一個外部可執行文件。謝謝。 – Ray
以前沒有人推薦過使用Process類嗎?它支持一個Exited事件和一個ExitCode屬性。 –