我正在尝试为我们公司的某些程序创建一个看门狗应用程序。如果关闭该程序,我已经可以启动该程序,但是我的最后一个问题是看门狗如何检测它监视的程序是否丢失。我想添加另一个条件,如果找不到该程序(例如Firefox),它将重新启动系统。谢谢
Module Module1
Public Sub Main()
Application.Run(New Watchdog)
End Sub
End Module
Public Class Watchdog
Inherits ApplicationContext
Private AppToWatch As String
Private FullPath As String = "C:\Program Files\Mozilla Firefox\firefox1.exe"
Private WithEvents P As Process
Public Sub New()
AppToWatch = System.IO.Path.GetFileNameWithoutExtension(FullPath)
Dim PS() As Process = Process.GetProcessesByName(AppToWatch)
If PS.Length = 0 Then
StartIt()
Else
P = PS(0)
P.EnableRaisingEvents = True
End If
End Sub
Private Sub P_Exited(sender As Object, e As EventArgs) Handles P.Exited
StartIt()
End Sub
Private Sub StartIt()
P = Process.Start(FullPath)
P.EnableRaisingEvents = True
End Sub
End Class
答案 0 :(得分:0)
保持应用程序是否正在运行的最简单方法是每5分钟或使用计时器以任意间隔检查一次进程,如果找不到,则可以调用“ StartIt()”,请确保您也启用了该计时器。