Check if program stop running

MiPakTeh 1,476 Reputation points
2021-08-24T00:39:18.46+00:00

Hi All,

To test wether that program "second.exe" are stop running using this code below.It seem not working.

Thank.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace mag_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ManagementEventWatcher stopWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));

            try
            {
                stopWatch.EventArrived += new EventArrivedEventHandler(this.Bulk_);
                stopWatch.Start();

            }

            catch (Exception)
            {
                MessageBox.Show("Can't start the Process-Monitor! Are you running as admin?");
                return;
            }

        }

        private void Bulk_(object sender, EventArrivedEventArgs e)
        {
            listBox1.Items.Add("Process stop: {0}" + e.NewEvent.Properties["Second.exe"].Value);
        }


    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,402 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-08-24T02:29:40.997+00:00

    If you need to check whether an application is closed, then we should use Win32_ProcessStopTrace instead of Win32_ProcessStartTrace.

    Moreover, the result object we get has no attribute named Second.exe, we should use ProcessName instead.

    Finally, cross-thread modification of the listbox is not allowed, we need to use Invoke.

     public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void Bulk_(object sender, EventArrivedEventArgs e)  
            {  
                Invoke(new MethodInvoker(() =>  
                {  
                    listBox1.Items.Add(string.Format("Process stop: {0}",e.NewEvent.Properties["ProcessName"].Value));  
                }));  
            }  
      
            private void Form1_Load(object sender, EventArgs e)  
            {  
                ManagementEventWatcher stopWatch = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));  
      
                try  
                {  
                    stopWatch.EventArrived += new EventArrivedEventHandler(this.Bulk_);  
                    stopWatch.Start();  
                }  
      
                catch (Exception)  
                {  
                    MessageBox.Show("Can't start the Process-Monitor! Are you running as admin?");  
                    return;  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful