如何在 VB.net 中重新运行可执行文件
我完全是VB.net的新手,现在我正在开发一个Windows服务,它将在启动时启动一个*.exe。如果此可执行文件的进程被其他程序杀死,我如何检测并重新启动该进程?
我的代码如下:
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
结束课程
提前非常感谢!
I am totally a newbie to VB.net, now I am developping a Windows Service, which will start an *.exe when starting. How can I detect and re-start a process of this executable if it got killed by some other program?
My code is as below:
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
End Class
Thanks a lot in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误的方法是在 OnStart() 方法中创建 Process 对象。将一个方法绑定到进程对象的 Exited 事件以重新启动该对象。 http://msdn.microsoft .com/en-us/library/system.diagnostics.process.onexited(VS.71).aspx 包含有关退出事件的一些信息。
正确的方法是创建一个 Timer 对象。每 X 分钟创建一个 Process 对象,检查句柄 ID,并查看它是否正在运行。如果没有,则创建一个新的可执行文件,类似于您在 OnStart() 方法中所做的操作。
http://csharpstruggles.blogspot.com/2005/ 02/using-timer-in-windows-service.html
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bc175c60-f81b-4d1a-975d-717368468238
优雅的方式是如上所述,在服务本身中完成工作。
The wrong way to do it is create a Process object in the OnStart() method. Tie a method to the Exited event of the process object that restarts the object. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.onexited(VS.71).aspx has some information on the Exited event.
The correct way to do it is create a Timer object. Every X amount of minutes, create a Process object, check for the handle ID, and see if it's running. If not, then create a new executable, similar to what you did on the OnStart() method.
http://csharpstruggles.blogspot.com/2005/02/using-timer-in-windows-service.html
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bc175c60-f81b-4d1a-975d-717368468238
The elegant way is, as mentioned above, to do the work in the service itself.
您可以使用 process.start 和 process.Exited 方法,如下所示:
You can use the process.start and the process.Exited methodes, like this: