The procedures in this topic walk you through the steps involved in creating a process, responding when a process stops, and stopping processes. In the first section, you will create a Windows application with Button controls to start and stop a Notepad process. You will start several instances of Notepad individually and then stop them as a group. In the second section, you will create a console application that enumerates the processes running on your computer.
For more information on using the Process component to interact with system processes, see Introduction to Monitoring and Managing Windows Processes.
To create your application
- From the New Project dialog box, create a Visual Basic or Visual C# Windows Application.
- With the Form1 designer open, click the Windows Forms tab in the Toolbox, and then add two buttons to the form.
- In the Properties window, change the following properties:
| Control | Property | Value |
| Button1 | Name | ButtonStart |
| | Text | Start Process |
| Button2 | Name | ButtonStop |
| | Text | Stop Process |
- Click the Component tab in the Toolbox, and then drag an instance of the Process component to the designer surface.
- Name the component myProcess.
To start the Notepad.exe process
- In the Properties window for the Process component instance, expand the StartInfo property, and set the FileName property to notepad.exe.
- Double-click the Start button to access the Code Editor, and then add the following code to the ButtonStart_Click() event:
' Visual Basic
myProcess.Start
// C#
myProcess.Start();
- Save all files, and then build and run the application.
- Click the Start Process button a few times. You will see separate instances of Notepad for each click.
- Close the individual Notepad applications.
- Close the Form1 application.
To close all current instances of the Notepad.exe process
- From Design view, double-click the Stop button to access the ButtonStop_Click() procedure.
- Add the following code to close the current instances of Notepad.
' Visual Basic
Private Sub ButtonStop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonStop.Click
Dim myProcesses() As Process
Dim instance As Process
myProcesses = Process.GetProcessesByName("Notepad")
For Each instance In myProcesses
instance.CloseMainWindow()
Next
End Sub
// C#
private void ButtonStop_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process[] myProcesses;
myProcesses =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.CloseMainWindow();
}
}
- Save all files, and then build and run your application.
- Start several instances of Notepad by clicking the Start Process button.
- Click the Stop Process button to immediately close all running instances of Notepad.
- Close the Form1 application.
To configure the component to wait for the Notepad process to finish running
In this procedure, you will freeze your code until the process finishes.
- Access the Code Editor for Form1.
- Modify the code by adding a line of code to cause a pause before closing each window.
' Visual Basic
Private Sub ButtonStop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonStop.Click
Dim myProcesses() As Process
Dim instance As Process
myProcesses = Process.GetProcessesByName("Notepad")
For Each instance In myProcesses
instance.WaitForExit(3000)
instance.CloseMainWindow()
Next
End Sub
// C#
private void ButtonStop_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process[] myProcesses;
myProcesses =
System.Diagnostics.Process.GetProcessesByName("Notepad");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.WaitForExit(3000);
instance.CloseMainWindow();
}
}
- Save all files, and then build and run the application.
- Start several instances of Notepad by clicking the Start Processes button.
- Click the Stop Processes button to close the processes as before.
You will notice that the application now waits 3 seconds for each process to stop before closing the next instance of the process.
- Close the Form1 application.
In this portion of the walkthrough, you will create a new console application that will retrieve and list the processes on the local computer.
To enumerate the processes on your computer
- Create a new Console Application project.
- In Solution Explorer, right-click the project and select Properties.
- On the General tab of the Property Pages dialog box, set the StartupObject property for your project to Sub Main. Note that the StartupObject in C# will be the class name which contains the Static Main Method.
- Open the Code Editor for the module, and then add the following code to your module to enumerate the processes on your computer:
' Visual Basic
Shared Sub Main()
Dim processes() As Process
Dim instance As Process
Dim process As New Process()
processes = process.GetProcesses
For Each instance In processes
Console.WriteLine(instance.ProcessName)
Next
System.Threading.Thread.Sleep(5000)
End Sub
// C#
static void Main()
{
System.Diagnostics.Process[] processes;
processes = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process instance in processes)
{
Console.WriteLine(instance.ProcessName);
}
System.Threading.Thread.Sleep(5000);
}
- Save all files, and then build and run your application.
The application will open a console window containing a list of all processes currently running on your computer. Calling Thread.Sleep pauses the console for 5 seconds before closing it.
Note You may receive exceptions in these two procedures if you call GetProcesses or GetProcessesByName and one of the returned processes finishes before you make the next call. In that case, you will receive an exception saying that the process does not exist.
See Also
Managing Processes | Starting Processes | Stopping Processes