Customized Task Manager in .NET Using C# and Windows Forms

All people who are working on any version of Windows Operating system know about Task Manager. They might be using Task Manager very frequently to start, end or manage any process. Even though, this Task Manager is been around for long time and it is commonly using tool in our daily work. But, it is still lacking some features which might be useful in certain situations. So, I designed this application to provide features which are not present in built-in Windows Task Manager.
        
I designed this application in VS.NET 2003 using C# and Windows Forms. First, I will explain some features present in this application followed by its design and coding.

Features present in this Customized Task Manager:

  • Displays all processes running in your system.
  • Allows you to add and remove any process (task) on your local machine.
  • Highlights current executing process.
  • Ability to connect to any machine and display its process's list.
  • Ability to set or get priority of any process running on your local machine.
  • Ability to monitor .NET CLR Managed memory.

Steps to create this application:

Create a new Windows Application in VS.NET 2003 using C# and name it as CustomizedTaskManager. Then, design the Main form (startup form) as shown below:

Image1.jpg

Image2.jpg

I placed a tab control with two tab pages. One is used to display Task Manager and another to display CLR Memory statistics. Than, I placed a ListView control in first tab page where all processes and its details are displayed. I added columns like process name, process id etc to the ListView.

Then I added MainMenu to provide following options:

  1. To popup Connect to Remote machine dialog.
  2. To popup Create New Task Window.
  3. To control highlighting of current executing processes.
  4. To End a selected process.
  5. To exit the application.

Than add a Performance counter component from toolbox and set its CategoryName as ".NET CLR Memory" and instance Name as "aspnet_wp" (you can keep instance name as any .NET managed application's first 15 characters in its Name).Add appropriate labels to tabpage2 as shown in above figure.

Than, add another form and name it as frmnewprcdetails and design it as shown below:

Image3.jpg 

To minimize coding, I am using this form for both displaying dialog for connecting to remote machine and add new task functionality.

This are the namespaces, I used in Form1:

System.Diagnostics
System.Threading

Initially, I thought of displaying all processes in ListView and update its details for every one second using Threading. Timer object. But, if we update like that for every second by clearing all ListView items and reloading them with new details will produce flicker on the form for every reload. So, in order to avoid that problem, I used a hashtable. The logic is:

I am loading all processes when the form loads. Later, I created a timer object with interval one second to call LoadAllProcesses(). In this method, first I am storing all currently running process's details in a hashtable using Process.GetProcesses(machinename). Than, I am comparing hashtable contents with each item present in ListView and if it process ID matches in both hashtable and ListView than we will update its details like processor time, number of threads etc and set ListView item's color to red if any of its processor time, memory usage is changed.

If any new process is created, than the count of ListView and HashTable items will not be equal. Based on this condition, I am adding an item to ListView control from hashtable's contents whose process ID is not present in ListView control.

If any process is ended, than I am removing that item from ListView by using ended process's ID. We can get ended process's ID by finding the items that are present in ListView but not in current process list (hashtable).

Like this, it will update all process's details in ListView control for every second without any flicker.

In order to create a new task, I am getting user selected task's path using frmnewprcdetails Form. We will create that process by calling Process.Start(taskname,arguments).

Than, I am providing functionality of ending selected process by calling Process.GetProcessById selectedprocessid,machinename).Kill().

I am providing remote machine's process load by setting mcname to selected machine's IP address from Connect To Remote Machine dialog. After getting IP address, I am reloading ListView with remote machine's process list.

We can get and set priority of any running process from context menu control. This context menu Set Priority will first make a check mark on correct MenuItem based on its priority. After the context menu popup, we can change its priority by clicking appropriate MenuItem (like Real-Time, Normal etc). In the status bar, I am displaying total count of processes and threads in each panel.

Than in tabpage2, I am displaying CLR memory details like heap size. Some applications won't run fast, if its objects are not garbage collected properly. This will help us to see total heap size and how much percent it is shared among Gen 0, Gen 1, and Gen 2 objects. This will help us to know whether our application is running slow because of insufficient CLR Memory. This will also helps us to take decision in calling Garbage Collector explicitly or not in our application code.
  
I added features Always on Top and Hide When Minimized in Options Menu. Finally, I added Notify Icon to show/hide Task Manager.

In order to load remote machine's process list, you must have administrative rights or its equivalent on that remote machine.

Final output will be like this:

Image4.jpg

Image5.jpg 

We can still enhance this application by providing more performance counters for processor, asp.net applications etc, good UI.

I am attaching code for this article for further reference. I hope this code will be useful for all.

Up Next
    Ebook Download
    View all
    Learn
    View all