Page 1 of 1

Working With Processes And Creating a Process Manager

#1 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Posted 16 April 2011 - 10:16 AM

Hello everybody at Dream.In.Code! Today this tutorial is going to focus on process and how to write a simple process manager in Visual C#! The things we will need of course are Visual C# 2010 Express or Visual Studio and that's about it so lets get started.

First of all lets create a new project and name it whatever you want. After that set any optional form properties like the title, size, or anything that you want custom. After that we are going to add a ListView control to the form. We are going to need to set a few properties of it. Here is what you will need to change:

Dock = Fill
View = Details
GridLines = True

After we have done that lets click on our columns ellipsis button so we can edit our columns. Lets add one column with the text "Process Name" without the quotes. You can set the width of the column to whatever you like.

Our form with the completed ListView should now look a little like this.
Posted Image

Now we have one last thing to do with the design and then it is on to coding. We are going to add a context menu strip so we can right click on a process to kill it. All we have to do for this is add a context menu strip from the tool box and drop it onto the form. Then we have to click on the ListView and go to the ContextMenuStrip property and double click (none) and that should set it to the context menu strip we added. After we have added the context menu strip lets add a "Refresh List" item and a "Kill Process(es)" item both without quotes. After we have done that our GUI should be completely finished and look a little like this:
Posted Image

Now it's time for my favorite part! The coding! To start out lets double click on the top of the form so we can go into the load event for the form. Before we do any code in the load event lets put a very important using at the top of the code. Insert this: using System.Diagnostics;. That is the base of the whole program pretty much. It allows us to use the Process class which allows us to manipulate processes. When we are in the load area of the code we are going to insert this snippet that will add all processes to the listview and sort them alphabetically. Here is the code:
            foreach (Process pro in Process.GetProcesses()) //loops through all processes
            {
                ListViewItem item = new ListViewItem(pro.ProcessName); //create a listviewitem with the text of the process name
                item.Tag = pro; //make the tag object the process so we can kill it easily
                listView1.Items.Add(item); //add the listview item to the process list
            }
            listView1.Sorting = SortOrder.Ascending; //makes the sort order a to z
            listView1.Sort(); //sorts process alphabetically


That is the main part of the code and if you want to run it and see what it looks like you will get something similar to this:
Posted Image

It is now complete besides the refresh list and kill functions on the context menu strip. So now lets put those in. First go to design view and click on the context menu strip at the bottom of the design view then double click on kill process like so:
Posted Image

After you do that all you have to do is insert this following code which will allow you to go through all the selected list items and then the snippet will end all of the selected ones.
Code:
            if (listView1.SelectedItems.Count >= 1) //makes sure there is at least one list item selected
            {
                foreach (ListViewItem item in listView1.SelectedItems) //iterates through all processes to kill
                {
                    ((Process)item.Tag).Kill(); //kills the process
                    item.Remove(); //removes the process from the list
                }
            }


After you do that go to the design view one more time and do the same as what you did for the kill process menu item and do it for refresh list. After you double click on it and you are in code view then insert this snippet which basically does the same as the load event except it clears the items first.
Code:
            listView1.Items.Clear(); //removes all items for refreshing
            //same code as load event
            foreach (Process pro in Process.GetProcesses()) //loops through all processes
            {
                ListViewItem item = new ListViewItem(pro.ProcessName); //create a listviewitem with the text of the process name
                item.Tag = pro; //make the tag object the process so we can kill it easily
                listView1.Items.Add(item); //add the listview item to the process list
            }
            listView1.Sorting = SortOrder.Ascending; //makes the sort order a to z
            listView1.Sort(); //sorts process alphabetically


After that you should be done. Congratulations :) You just made a process manager that can view and kill processes. I hope you enjoyed this tutorial and give me some feedback please :)

Is This A Good Question/Topic? 2
  • +

Page 1 of 1