שתף באמצעות


VB.NET: Check To See If A Process Is Running

Question

Friday, February 10, 2012 8:01 PM

Hi, I'm not sure how I would use Process.GetProcessesByName() to check if a process is running, and if it isn't start it, using VB.NET?  I know how to start a process using Process.Start().

Help?

Thanks
Aaron

All replies (3)

Friday, February 10, 2012 8:11 PM ✅Answered | 7 votes

What Process.GetProcessesByName() does is indeed to see if there are any processes that run with a specified name, how many etc.

I recommend using this code:

Dim p() As Process

Private Sub CheckIfRunning()
    p = Process.GetProcessesByName("processName")
    If p.Count > 0 Then
        ' Process is running
    Else
        ' Process is not running
    End If
End Sub

This code will search for the process "processName.exe", so don't manually enter ".exe" in the process name.

- bilde2910 :)


Saturday, February 11, 2012 12:28 AM

Thanks for that.

Aaron


Monday, March 10, 2014 3:19 AM

'I found .Count is not allowed in VS2010 use '.Length' Instead

 Private Function CheckIfRunning(sProcessName As String) As Boolean
        Dim bRet As Boolean = False
        Try
            Dim listProc() As System.Diagnostics.Process
            listProc = System.Diagnostics.Process.GetProcessesByName(sProcessName)
            If listProc.Length > 0 Then
                ' Process is running
            Else
                ' Process is not running
            End If
        Catch ex As Exception
            GL.Excep("Frm_Main_SRV.CheckIfRunning", ex)       

        End Try
        Return bRet
    End Function