Walkthrough: Managing a Windows Process
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.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
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:
myProcess.Start
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.
Security Note: If you are running in a partial-trust context, the process might throw a SecurityException exception due to insufficient privileges. For more information, see Code Access Security Basics.
Close the individual Notepad applications.
Close the Form1 application.
To close all current instances of the Notepad.exe process
Access the Code Editor for Form1.
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.
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() instance.Close() Next End Sub
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(); instance.Close(); } }
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.
In the next procedure, you will freeze your code until the process finishes.
To configure the component to wait for the Notepad process to finish running
Access the Code Editor for Form1.
Modify the code by adding a line of code to cause a pause before closing each window.
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() instance.WaitForExit(3000) instance.Close() Next End Sub
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(); instance.WaitForExit(3000); instance.Close(); } }
Save all files, and then build and run the application.
Start several instances of Notepad by clicking the Start Processes button.
Click the Stop Processesbutton 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.
Open the Code Editor and modify the Main method to enumerate the processes on your computer:
Sub Main() Dim processes() As Process Dim instance As Process processes = Process.GetProcesses For Each instance In processes Console.WriteLine(instance.ProcessName) Next System.Threading.Thread.Sleep(5000) End Sub
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 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.