Show list of processes and how to Close it (Kill Process)

Use this board to post your code snippets - tips and tricks
6 posts Page 1 of 1
Contributors
MANSQN
VIP - Donator
VIP - Donator
Posts: 159
Joined: Sat Sep 17, 2011 11:33 pm

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

Fist add the following to the very top of the code page:
Code: Select all
Imports System.Diagnostics
Add the following code to Button2.Click event (Get List Button)
Code: Select all
ListBox1.Items.Clear()

Dim a As System.Diagnostics.Process

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

ListBox1.Items.Add(a.ProcessName)

Next
Add the following code to Button1.Click event (End Process)
Code: Select all
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
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

It is not clear why all that unneeded code is in the end process function.

You could use this:
Code: Select all
        If ListBox1.SelectedIndex > -1 Then
            Dim procs As Process() = Process.GetProcessesByName(ListBox1.SelectedItem.ToString())
            For i As Integer = 0 To procs.Length - 1
                procs(i).Kill()
            Next

            Button2.PerformClick()
        End If
User avatar
lesan101
Top Poster
Top Poster
Posts: 193
Joined: Tue Jun 29, 2010 8:10 am

oh nice tut, once i get home i will make one. thanks !
Image
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

First of all its a quick snip no tut. Second please dont bump unless you got a good question (mandai's post dated from 20 september 2011).

Sincerely,
Clanc789
Practice makes perfect!

VIP since: 6-10-2011
User avatar
lesan101
Top Poster
Top Poster
Posts: 193
Joined: Tue Jun 29, 2010 8:10 am

clanc789 wrote:
First of all its a quick snip no tut. Second please dont bump unless you got a good question (mandai's post dated from 20 september 2011).

Sincerely,
Clanc789
wow shit sorry if that got you mad? i never look at the date thats why.
Image
User avatar
clanc789
Coding Guru
Coding Guru
Posts: 786
Joined: Tue Nov 02, 2010 4:45 pm

lesan101 wrote:
clanc789 wrote:
First of all its a quick snip no tut. Second please dont bump unless you got a good question (mandai's post dated from 20 september 2011).

Sincerely,
Clanc789
wow shit sorry if that got you mad? i never look at the date thats why.
I doest get me mad, just that i dont want you that other members -rep you for this. A red block under your name doesnt help you when you ask help in the forum. Just FYI.
Practice makes perfect!

VIP since: 6-10-2011
6 posts Page 1 of 1
Return to “Quick Snips”