Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constantly monitor if a process is running

I have the following code:

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

And it works GREAT!

But I'm wondering how I would convert this to a monitoring application, to constantly check if the processes is running. Is it as simple as putting the check on a timer every 1 second, or is there a better, more efficient way to go about this.

In the end result, I'd like to have a label that says "Running", or "Not Running" based on the process, but I need something to watch the process constantly.

like image 331
level42 Avatar asked Jan 29 '26 22:01

level42


2 Answers

If you need the app running all the time, then you don't need a Timer at all. Subscribe to the Process.Exited() event to be notified when it closes. For instance, with Notepad:

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

That would keep it open all the time, assuming you wanted to open it if it wasn't already running.

If you don't want to open it yourself, and need to detect when it does open, then you could use WMI via the ManagementEventWatcher as in this previous SO question.

like image 184
Idle_Mind Avatar answered Jan 31 '26 12:01

Idle_Mind


I've done something similar to this to monitor an exe that I need to be running all the time, and to restart it if it was down. Mine was running as a Windows Service - that way it would start when windows booted and id never need to look after it.

Alternatively you could just create it as a console app and put it in your startup folder?

I had:

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

My "server" app was called Lorraine...Also a timer maybe better practice than having the thread sleep..

like image 27
Shovers_ Avatar answered Jan 31 '26 11:01

Shovers_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!