持续监控进程是否正在运行

时间:2015-08-13 15:32:00

标签: vb.net timer process monitor

我有以下代码:

Dim p() As Process

Private Sub CheckIfRunning()
    p = Process.GetProcessesByName("skype") 'Process name without the .exe
    If p.Count > 0 Then
        ' Process is running
        MessageBox.Show("Yes, Skype is running")
    Else
        ' Process is not running
        MessageBox.Show("No, Skype isn't running")
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    CheckIfRunning()
End Sub

它的效果很棒!

但我想知道如何将其转换为监控应用程序,以不断检查进程是否正在运行。是否像每1秒钟对一个计时器进行检查一样简单,或者是否有更好,更有效的方法来解决这个问题。

在最后的结果中,我想要一个标签上写着" Running"或" Not Running"基于这个过程,我需要不断观察这个过程。

3 个答案:

答案 0 :(得分:4)

如果您需要应用程序一直运行,那么您根本不需要计时器。订阅Process.Exited()活动,以便在关闭时收到通知。例如,使用记事本:

Public Class Form1

    Private P As Process
    Private FileName As String = "C:\Windows\Notepad.exe"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ps() As Process = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(FileName))
        If ps.Length = 0 Then
            P = Process.Start(FileName)
            P.EnableRaisingEvents = True
            AddHandler P.Exited, AddressOf P_Exited
        Else
            P = ps(0)
            P.EnableRaisingEvents = True
            AddHandler P.Exited, AddressOf P_Exited
        End If
    End Sub

    Private Sub P_Exited(sender As Object, e As EventArgs)
        Console.WriteLine("App Exited @ " & DateTime.Now)
        Console.WriteLine("Restarting app: " & FileName)
        P = Process.Start(FileName)
        P.EnableRaisingEvents = True
        AddHandler P.Exited, AddressOf P_Exited
    End Sub

End Class

这会让它一直保持打开状态,假设你想打开它,如果它还没有运行的话。

如果您不想自己打开它,并且需要检测它何时打开,那么您可以通过此ManagementEventWatcher中的previous SO question使用WMI。

答案 1 :(得分:2)

我已经做了类似的事情来监控我需要一直运行的exe,并在它关闭时重新启动它。 我的Windows服务正在运行 - 这样它将在Windows启动时启动,而id永远不需要照顾它。

或者您可以将其创建为控制台应用程序并将其放在启动文件夹中?

我有:

Sub Main()

    Do
        Check_server()
        Dim t As New TimeSpan(0, 15, 0)
        Threading.Thread.Sleep(t)
    Loop

End Sub


Public Sub Check_server()

    Dim current_pros() As Process = get_pros()
    Dim found As Boolean = False

    If Now.Hour < "22" Then
        For Each pro In current_pros
            If pro.ProcessName.ToLower = "Lorraine" Then
                found = True
                Exit For
            Else
                found = False
            End If
        Next
        If found Then
            Console.WriteLine("Server up")
        Else
            Console.WriteLine("Server down - restarting")
            restart_server()
        End If
    End If
End Sub

我的&#34;服务器&#34;应用程序被称为Lorraine ...也许一个计时器可能比线程睡眠更好的练习..

答案 2 :(得分:1)

根据我的经验,一个简单的计时器效果最好:

'Timer interval set to 1-5 seconds... no remotely significant CPU hit
Private Sub timerTest_Tick(sender As System.Object, e As System.EventArgs) Handles timerTest.Tick
    Dim p() As Process = Process.GetProcessesByName("Skype")
    lblStatus.Text = If(p.Length > 0, "Skype is running.", "Skype isn't running.")
End Sub

您的里程可能会有所不同,但除非必要,否则我不想处理单独的线程。