如何在 VB.net 中重新运行可执行文件

发布于 2024-08-21 00:17:50 字数 1041 浏览 2 评论 0原文

我完全是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鼻尖触碰 2024-08-28 00:17:50

错误的方法是在 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.

樱桃奶球 2024-08-28 00:17:50

您可以使用 process.start 和 process.Exited 方法,如下所示:

Imports System.Diagnostics

Public Class Form1
Dim WithEvents proc As Process

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
  Handles Button1.Click
' start the process
proc = Process.Start(progName)
proc.EnableRaisingEvents = True
End Sub

Private Sub proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles proc.Exited
' restart the process whenever it exits
proc = Process.Start(progName)
proc.EnableRaisingEvents = True
End Sub

End Class

You can use the process.start and the process.Exited methodes, like this:

Imports System.Diagnostics

Public Class Form1
Dim WithEvents proc As Process

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
  Handles Button1.Click
' start the process
proc = Process.Start(progName)
proc.EnableRaisingEvents = True
End Sub

Private Sub proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles proc.Exited
' restart the process whenever it exits
proc = Process.Start(progName)
proc.EnableRaisingEvents = True
End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文