我完全是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属性。 –