How to determine when a shelled process ends in Visual Basic .NET or in Visual Basic 2005 (306394)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q306394
For a Microsoft Visual C# .NET version of this article, see 304032.
For a Microsoft Visual C++ .NET version of this article, see 307011.
For a Microsoft Visual Basic 6.0 version of this article, see 129796.

IN THIS TASK

SUMMARY

This article demonstrates 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.

back to the top

Requirements

  • Visual Studio .NET or Visual Studio 2005
  • Visual Basic .NET or Visual Basic 2005 Language Compiler
back to the top

Create the Sample

  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows Application project. Form1 is created by default.
  2. Double-click Form1, and add the following code to the Form1 Load event:
            p = New Process()
            ' Handle the Exited event that the Process class fires.
            AddHandler p.Exited, AddressOf p_Exited
            p.EnableRaisingEvents = True
            p.SynchronizingObject = Me
            p.StartInfo.FileName = "notepad.exe"
            p.Start()
    					
  3. Below the Form1_Load method, add another method that will handle the Exited event:
        Private Sub p_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
            MessageBox.Show("Notepad was closed")
        End Sub
    					
  4. Add the following declaration to the Form1 class to declare your process variable:
        Dim WithEvents p As Process
    					
  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.
back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbDiagnostics kbHOWTOmaster KB306394 kbAudDeveloper