Show list of processes and how to Close it (Kill Process) - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

For this Tutorial we need a Listbox and 2 Buttons
We will use the Listbox to display all the process that are running:
Button1 we be used to end the selected process from the list
Button2 will be used to get all the process and display them in the list box

Show list of processes and how to Close it- vb.net

Fist add the following to the very top of the code page:

Imports System.Diagnostics


Add the following code to Button2.Click event (Get List Button)

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles Button2.Click
     ListBox1.Items.Clear()
     Dim a As System.Diagnostics.Process
	 For Each a In System.Diagnostics.Process.GetProcesses()
            ListBox1.Items.Add(a.ProcessName)
     Next
End Sub


Add the following code to Button1.Click event (End Process)

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles Button1.Click
        Try
            Dim a As System.Diagnostics.Process
            For Each a In System.Diagnostics.Process.GetProcesses()
                Dim list() As String = ListBox1.SelectedItem.ToString().Split("-")
                Dim Process As String = list(0).Trim()
                Dim iId As Integer = Convert.ToInt32(list(1).Trim())
                If a.ProcessName = Process And a.Id = iId Then
                    a.Kill()
                End If
            Next
            Button2.PerformClick()
        Catch ex As Exception
            MsgBox("Please Select a Process to kill from the list")
        End Try
    End Sub
    


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.