question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked TimonYang-MSFT answered

Check if program stop running

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);
         }
    
    
     }
 }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.