Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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.

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:
    ControlPropertyValue
    Button1NameButtonStart
     TextStart Process
    Button2NameButtonStop
     TextStop 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:
    ' Visual Basic
    myProcess.Start
    
    // C#
    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 Note   If you are running in a partial-trust context, the process might throw a SecurityException Class 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. From Design view, double-click the Stop button to access the ButtonStop_Click() procedure.
  2. 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();
       }
    }
    
  3. Save all files, and then build and run your application.
  4. Start several instances of Notepad by clicking the Start Process button.
  5. Click the Stop Process button to immediately close all running instances of Notepad.
  6. 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.

  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.
    ' 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();
       }
    }
    
  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 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.

  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. In Solution Explorer, right-click the project and select Properties.
  3. 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.
  4. 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);
    }
    
  5. 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

Show:
© 2015 Microsoft