Article ID: 304032 - Last Review: December 11, 2006 - Revision: 2.2

How to determine when a shelled process ends in Visual C#

System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q304032

On This Page

Expand all | Collapse all

SUMMARY

This step-by-step article shows you how to use the Process object to start another process and receive a notification after that process has exited. The Process object, which is located in the System.Diagnostics namespace, exposes the Exited event. By handling this event, you are notified when the process that you launch has exited. Because you are using an event to accomplish this task, this notification process is asynchronous.

Requirements

  • Visual Studio .NET or Visual Studio 2005
  • Visual C# Language Compiler

Create the Sample

  1. Create a new Visual C# .NET Windows Application project or a new Visual C# 2005 Windows Application project. Form1 is created by default.
  2. Double-click Form1, and add the following code to the Form1 Load event handler:
    p = new System.Diagnostics.Process();
    // Handle the Exited event that the Process class fires.
    this.p.Exited += new System.EventHandler(this.p_Exited);
    p.EnableRaisingEvents = true;
    p.SynchronizingObject = this;
    p.StartInfo.FileName = "notepad.exe";
    p.Start(); 
    					
  3. Below the Form1_Load method, add another method that will handle the Exited event:
    private void p_Exited(object sender, System.EventArgs e)
    {
       MessageBox.Show("Notepad was closed");
    }
    					
  4. Add the following declaration to the Form1 class to declare your process variable:
        private System.Diagnostics.Process p;
    					
  5. Press F5 to run the application. The application should start, along with an instance of Notepad.
  6. Close Notepad. Note that the Exited event fires, which causes a message box to appear.

APPLIES TO
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB304032