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
- Visual Studio .NET or Visual Studio 2005
- Visual Basic .NET or Visual Basic 2005 Language Compiler
Create the Sample
- Create a new Visual Basic .NET or Visual Basic 2005 Windows Application project.
Form1 is created by default.
- 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"
- 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")
- Add the following declaration to the Form1 class to declare your process variable:
Dim WithEvents p As Process
- Press F5 to run the application. The application should
start, along with an instance of Notepad.
- Close Notepad. Note that the Exited event fires, which causes a message box to appear.