Hack Forums
[.NET] [VB.Net] Handling multiple processes at once, using for each and arrays - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [VB.Net] Handling multiple processes at once, using for each and arrays (/showthread.php?tid=888142)



[VB.Net] Handling multiple processes at once, using for each and arrays - The-One - Dec 11, 2010

Hello,

In this tutorial I will explain you how you can kill(you can do whatever you want, but i'll be using kill) multiple processes at once.

I'll be using:

- one Listview
- one Contextmenustrip

Okey,

First draw the items into the form.

Then, on top, under Public Class Form1 add:
Code
Dim countitems As Integer = 0

Then in Form1_Load paste:
Code
For Each p As Process In Process.GetProcesses
    Dim lviList As New ListViewItem(New String() {p.ProcessName.ToString}) ' create a new line
    ListView1.Items.Add(lviList) 'add it
    countitems = countitems + 1
  Next

Click the contextmenu you previously added and add a new item name it Termiate (or whatever you want).
Okey, go into the designer, select the listview.

Set the next settings:

CheckBoxes = True(To put checkboxes infront of the items, you can use multiselect (Hit Ctrl and select multiple items, but I prefer checkboxes))
View = Details
ContextMenuStrip = ContextMenuStrip1(So where ever you right click (inside the listview ofcourse), the contextmenustrip will appear)

Add the following code to the contextmenu item.

Code
Dim index As Integer = 0 ' count
  Dim str(0 To countitems) As String ' Max
  Array.Clear(str, str.GetLowerBound(0), str.Length) ' clear the array
  For Each lvi As ListViewItem In ListView1.CheckedItems ' for each checkbox checked
    str(index) = lvi.Text ' set array
    index = index + 1 ' index + 1 for next array
  Next
  For Each p As Process In Process.GetProcesses
    For i = 0 To index - 1 ' Loop through all process strings we want to kill
    If p.ProcessName.Contains(str(i)) Then ' if its equal
    p.Kill() ' kill it
    End If
    Next
  Next

You can use multiselect to, you just have to change:
Code
For Each lvi As ListViewItem In ListView1.CheckedItems
into
Code
For Each lvi As ListViewItem In ListView1.SelectedItems

and ofcourse, leave the checkboxes out.

Press F5 and check it out.

Fully programmed and wrote by me, reply's are welcome :)


* - ident - Dec 11, 2010

Variable += Value

Simple really.


* - The-One - Dec 11, 2010

(Dec 11, 2010 08:50 AM)ident Wrote:  Variable += Value

Simple really.

Nice explanation! /sarcasm.


* - ident - Dec 11, 2010

I was just pointing it out. Back when i started programming i learned variable basics before Process.GetProcesses Method.


* - The-One - Dec 11, 2010

(Dec 11, 2010 09:03 AM)ident Wrote:  I was just pointing it out. Back when i started programming i learned variable basics before Process.GetProcesses Method.

Lol you are comparing variables with processes? Like comparing a dog with a cupcake.


* - ident - Dec 11, 2010

I can see the point went straight over your head. Should of guessed it really.


* - The-One - Dec 11, 2010

(Dec 11, 2010 09:07 AM)ident Wrote:  I can see the point went straight over your head. Should of guessed it really.

Well done, can you now get the fk out of my thread?