Click here to Skip to main content
Click here to Skip to main content

Killing Processes from a Visual Basic Application

, 9 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article provides a simple example of how to use the System.Diagnostics.Process library to display a list of running processes, and to select and kill processes by their process name and ID.

Introduction

This article provides a simple example of how to use the System.Diagnostics.Process library to display a list of running processes, and to select and kill processes by their process name and ID. It would not be a major trick to display additional information about the process using the same library or to kill processes by their ID or name alone rather than their name and ID but in this example, process names and IDs are used for display purposes and the purpose of killing a running process.

image001.png
Figure 1: Listing and Killing Processes by Process Name and ID

Getting Started

There is a single solution included with this download, the solution contains a Win Forms project called “ProcessKillerVB”; this project contains one form (Form1.vb) and all of the code required to display and kill processes is contained within that single form class. If you open the attached project into Visual Studio 2008; you should see the following in the solution explorer:

image002.png
Figure 2: Solution Explorer

Code: The Main Form

The main form contains all of the user interface elements necessary to display and update the list of currently running processes found on the user’s machine. It also contains a button handler which will kill a process by the process name. You can also kill a process by its ID but in terms of readability, if the user is selecting the process, the process name is going to make a lot more sense to them than will the process ID.

The class begins with the normal and default imports:

Imports System.Diagnostics

The constructor calls a method called “UpdateProcessList” which is used to get and display a list of running processes; it will be described shortly.

Public Class Form1


    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' display the list of running processes
        UpdateProcessList()

    End Sub

The next bit of code is the UpdateProcessList function; this function clears the listbox of any existing entries and then loops through each of the current processes, adding the name and ID of each process to the list (as Process Name – ID). At the end of the function, a status message is updated to display the current number of found processes.

    ''' <span class="code-SummaryComment"><summary></span>
    ''' Loop through the list of running processes
    ''' and add each process name to the process
    ''' listbox
    ''' <span class="code-SummaryComment"></summary></span>
    ''' <span class="code-SummaryComment"><remarks></remarks></span>
    Private Sub UpdateProcessList()

        ' clear the existing list of any items
        lstProcesses.Items.Clear()

        ' loop through the running processes and add
        'each to the list
        Dim p As System.Diagnostics.Process

        For Each p In System.Diagnostics.Process.GetProcesses()
            lstProcesses.Items.Add(p.ProcessName & " - " & p.Id.ToString())
        Next

        lstProcesses.Sorted = True

        ' display the number of running processes in
        ' a status message at the bottom of the page
        tslProcessCount.Text = "Processes running: " & _
        lstProcesses.Items.Count.ToString()

    End Sub

The next bit of code in the application is the update button’s click event handler; all it does is to call the UpdateProcessList function to update the contents of the listbox.

    ''' <span class="code-SummaryComment"><summary></span>
    ''' Manually update the list of runnings
    ''' processes
    ''' <span class="code-SummaryComment"></summary></span>
    ''' <span class="code-SummaryComment"><param name="sender"></param></span>
    ''' <span class="code-SummaryComment"><param name="e"></param></span>
    ''' <span class="code-SummaryComment"><remarks></remarks></span>
    Private Sub btnUpdateProcessList_Click(ByVal sender As System.Object, _
                                           ByVal e As System.EventArgs) _
                                           Handles btnUpdateProcessList.Click
        UpdateProcessList()

    End Sub

The last bit of the code in the application is used to kill a running process. Since you can have multiple processes with the same name, it was necessary to add in the ID along with the name in order to make some distinction between multiple instances of a single application, else, we would end up killing all running instances of the application which may or may not be desirable depending upon what you are doing. If you do want to kill off all running instances of an application, disregard the ID and kill all running processes with the same process name.

In the kill button click event handler; we again loop through all of the running processes and we take the selected item from the process/ID list, and parse it into a string containing the process name and an integer value containing the ID. If the current process (in the loop) matches on both the process name and process ID, the process is killed. At the end, the handler calls the update method used to repopulate the listbox such that is displays the current list of active processes and their IDs.

    ''' <span class="code-SummaryComment"><summary></span>
    ''' Kill the process selected in the process name
    ''' and ID listbox
    ''' <span class="code-SummaryComment"></summary></span>
    ''' <span class="code-SummaryComment"><param name="sender"></param></span>
    ''' <span class="code-SummaryComment"><param name="e"></param></span>
    ''' <span class="code-SummaryComment"><remarks></remarks></span>
    Private Sub btnKill_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnKill.Click

        If lstProcesses.SelectedItems.Count <= 0 Then
            MessageBox.Show("Click on a process name to select it.", "No Process 
            Selected")
            Return
        End If

        ' loop through the running processes looking for a match
        ' by comparing process name to the name selected in the listbox
        Dim p As System.Diagnostics.Process

        For Each p In System.Diagnostics.Process.GetProcesses()

            Dim arr() As String = _
            lstProcesses.SelectedItem.ToString().Split("-")

            Dim sProcess As String = arr(0).Trim()
            Dim iId As Integer = Convert.ToInt32(arr(1).Trim())

            If p.ProcessName = sProcess And p.Id = iId Then
                p.Kill()
            End If

        Next

        ' update the list to show the killed process
        ' has been removed
        UpdateProcessList()

    End Sub


End Class

That concludes the description of all of the code used in this example.

Summary

The article shows one approach that may be used to display a list of current processes, and to single out and kill a single process based upon both the process name and its ID. One could also kill all instances of a particular process by killing all processes that match on name without giving any consideration to the process ID. The approach shown allows for singling out specific instances of a process and killing it without taking down all other matching processes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionHow to use this for a remote workstation PinmemberMember 1096933324-Jul-14 9:12 
Questionyou need a donate button! Pinmembercellurl11-Jan-13 4:59 
GeneralUpdate PinmemberJoe Sutphin1-May-09 4:20 
GeneralSo VB is the reason why my pc crashes =) Pinmemberastanton197823-Jun-08 2:27 
GeneralYour article is not full PinmemberCuchuk Sergey17-Jun-08 7:24 
GeneralRe: Your article is not full PinmemberNeo-Code23-Jul-08 18:08 
GeneralRe: Your article is not full PinmemberCuchuk Sergey23-Jul-08 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 9 Jun 2008
Article Copyright 2008 by salysle
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid