I need some help with checking a process or if a certain window name is open on my project.

Right now what I have is:
FSXCheck = Timer
fsx.exe = Flight Simulator X

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FSXCheck.Enabled = True

    End Sub

    Private Sub FSXCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FSXCheck.Tick
        Dim objWMIService, colProcesses
        Dim Process As String
        Process = "fsx.exe"
        objWMIService = GetObject("winmgmts:")
        colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
        If colProcesses.Count Then
            ToolStripStatusLabel1.Text = "FSX Status: Connected"
        End If
    End Sub

I Have the process down, but I need to figure out the

If colProcesses.Count Then
            ToolStripStatusLabel1.Text = "FSX Status: Connected"
        End If

And try to get it to say "FSX Status: Not Connected" when the process is not running, that is what I have a problem on.

If anyone could help, that'd be awesome. :)

Dani AI

Generated

As pointed out, the quick fix was to test whether the process collection contains any items — and confirmed that solved the immediate problem. For production code, a simpler and faster approach than WMI is to use System.Diagnostics. Two common patterns are: 1) check for the process by name, and 2) check open window titles if you need to detect a specific UI.

' check by process name (note: use name without ".exe")
Dim procs() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("fsx")
If procs.Length > 0 Then
    ToolStripStatusLabel1.Text = "Found FSX process"
Else
    ToolStripStatusLabel1.Text = "FSX not running"
End If

If the goal is "is that window open", inspect MainWindowTitle. Wrap property access in a Try/Catch because some processes throw on access or return an empty title.

Dim found As Boolean = False
For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
    Try
        If p.MainWindowTitle.IndexOf("Flight Simulator X", StringComparison.OrdinalIgnoreCase) >= 0 Then
            found = True
            Exit For
        End If
    Catch ex As Exception
        ' ignore processes we cannot query
    End Try
Next
' update the status label based on found

Notes and gotchas: Process.GetProcessesByName expects the base name (no ".exe"). Use a reasonable timer interval (1s–5s) — querying too often wastes CPU. System.Windows.Forms.Timer runs on the UI thread so UI updates are safe; if you use a background timer or Task, marshal back with Invoke/BeginInvoke. For immediate detection, attach to a Process.Exited handler (set EnableRaisingEvents = True) for instances you start or enumerate and remember to remove handlers and dispose when done.

Recommended Answers

All 3 Replies

Hi,

Just taking a guess here but what does colProcesses.Count actually return?
How about:

If colProcesses.Count > 0 Then
   ToolStripStatusLabel1.Text = "FSX Status: Connected"
Else
      ToolStripStatusLabel1.Text = "FSX Status: Not Connected"
End if

OR

If colProcesses.Count IsNot Nothing then
 ToolStripStatusLabel1.Text = "FSX Status: Connected"
Else
      ToolStripStatusLabel1.Text = "FSX Status: Not Connected"
End if

Thanks man that worked, :)

Glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.