Managing your computer's processes
Introduction
In order to follow this tutorial step-by-step you will need to have .NET
Framework and Visual Studio .NET installed on your computer. If you only want to run
the sample application provided with this tutorial, then you will need only
.NET Framework.
Getting to work
Today, we will create a sample application witch is similar to Task
Manager,
that can help you understand better the Process class of the .NET
Framwork.
Let's get to work, shall we? Open Visual Studio .NET (if you
didn't already have it open), and create a new C# Windows Application ( File
-> New
Project ->Visual C# Projects -> Windows Application ). Name
it whatever you like (I called it ProcessManager). Visual
Studio .NET creates
a new project
adds to the project a Form.
If all worked well, you are now in front of the Design view.
Here, let's add a few controls to our form.
The first thing we need to add is a ListBox control, in witch
we will display the processes. So go to the Toolbox (if is
not visible, go to View
-> Toolbox or type Ctrl + Alt +X ) and drag-and-drop
the control on your form. Bellow it, we will add three buttons. Add the first
one. Set its Text property to
End Process and its Name to btn_end.
Let's add another button. Set its
Text to Refresh and its Name to btn_refresh. Finally, for
the third button, let's set its Text to Show Details and its Name to btn_details.
And that's about it for the interface. Here is how mine came out:
Again, the layout of the controls is not the issue here, so if yours came
out differently, don't worry. It will still work.
Before we get down to coding, you will need in import the System.Diagnostics namespace.
We want that when the form loads, to show us the names of the processes. So
we will need to create a method that occurs when the form loads. Let's
do that by double-clicking on the form's surface. You will notice a newly
created method called Form1_Load. In this method, we will
add the code to populate the listbox with the processes that are currently
in memory. We will need to
repopulate the ListBox also when we click refresh, or after
we end a process. So let's create a function. I will call it DoRefresh. Here
it is:
|
private void DoRefresh()
{
listBox1.Items.Clear();
Process[]
procs = Process.GetProcesses( Environment.MachineName );
foreach
(Process process in procs)
{
listBox1.Items.Add(process.ProcessName);
}
}
|
What does it do? Well, first of all it clears the items currently in the
ListBox. Then it create an array of Process objects,
witch it fills with the current processes taken from the current machine. Then
iterates through each
Process object in the array, and adds the Process' name
to the ListBox. So now let's call this method in the Form1_Load method:
Go now to the Design view and double-click the Refresh button. In the newly
created method, add a call to the DoRefresh method.
Again, go to Design view and double-click the End Process button. In the
new method, let's identify the project that was selected to be killed.
|
foreach (Process p in
Process.GetProcesses(Environment.MachineName))
{
if
(p.ProcessName.Equals(listBox1.SelectedItem.ToString()))
{
p.Kill();
break;
}
}
|
Again, we will iterate through the processes on our machine and find a process
that has the same name as our selected item in the listbox control. If we find
it, we kill it and we break out of the foreach loop.
In the Design view again, let's double-click the Show
Details button.
In the new method, we will need it iterate again through the current running
processes on the local machine, and find the Process who's
name is selected in the ListBox.
|
foreach
(Process p in Process.GetProcesses(Environment.MachineName))
{
if
(p.ProcessName.Equals(listBox1.SelectedItem.ToString()))
{
MessageBox.Show("Process Id: "+p.Id+"\n"+
"Process Name: "+p.ProcessName+"\n"+
"Handles Opened by the Process: "+p.HandleCount+"\n"+
"Main Window Title: "+p.MainWindowTitle+"\n"+
"Start Time: "+p.StartTime.ToShortDateString() +" "+
p.StartTime.ToShortTimeString()+"\n"+
"Threads Count: "+p.Threads.Count+"\n"+
"Phisical Memory: "+p.WorkingSet/1024+ " K"
);
break;
}
}
|
Once we find it, let's print some information about is, like its ID,
its name and so one. Notice that for the physical memory, I've divided
the result with 1024. The reason I've done that is the WorkingSet property
returns the current memory for the specified process in bytes. That
is a rather long and less impressive number, so I've changed it to show kilobytes.
Here is the application at work on my computer:
And that's it. Until next time, good luck and happy programming.
Raul POPESCU
Attachments:
Project Files: ProcessManager.zip