Share via


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

  1. From the New Project dialog box, create a Visual Basic or Visual C# Windows Application.

  2. With the Form1 designer open, click the Windows Forms tab in the Toolbox, and then add two buttons to the form.

  3. In the Properties window, change the following properties:

    Control

    Property

    Value

    Button1

    Name

    ButtonStart

     

    Text

    Start Process

    Button2

    Name

    ButtonStop

     

    Text

    Stop Process

  4. Click the Component tab in the Toolbox, and then drag an instance of the Process component to the designer surface.

  5. Name the component myProcess.

To start the Notepad.exe process

  1. In the Properties window for the Process component instance, expand the StartInfo property, and set the FileName property to notepad.exe.

  2. 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();
    
  3. Save all files, and then build and run the application.

  4. Click the Start Process button a few times. You will see separate instances of Notepad for each click.

    Security noteSecurity 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.

  5. Close the individual Notepad applications.

  6. Close the Form1 application.

To close all current instances of the Notepad.exe process

  1. Access the Code Editor for Form1.

  2. From Design view, double-click the Stop button to access the ButtonStop_Click() procedure.

  3. 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();
       }
    }
    
  4. Save all files, and then build and run your application.

  5. Start several instances of Notepad by clicking the Start Process button.

  6. Click the Stop Process button to immediately close all running instances of Notepad.

  7. 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

  1. Access the Code Editor for Form1.

  2. 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();
       }
    }
    
  3. Save all files, and then build and run the application.

  4. Start several instances of Notepad by clicking the Start Processes button.

  5. 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.

  6. 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

  1. Create a new Console Application project.

  2. 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);
    }
    
  3. 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.

See Also

Tasks

How to: Specify Processes

How to: Stop Processes

Other Resources

Managing Processes