Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#
Article

My TaskManager

,
Rate me:
Please Sign up or sign in to vote.
3.58/5 (13 votes)
30 Aug 2007CPOL3 min read 67.6K   3.2K   30   12
A task manager with additional functionality
Screenshot - task.jpg

Introduction

I know that on Code Project there are a lot of process managers programmed by brilliant programmers. But I need some more functionally in these process managers. Therefore I created my own task manager.

Background

Why I created this process manager, the reason is that sometimes we need to release our memory in just a short time. It takes time to select unnecessary processes from the Windows task managers and kill them one by one. While running some games or any other programs that require high RAM when you have shortage of RAM, this poses a problem. You'll want to use the RAM by only system processes and your favorite processes.

Point of Interest

You can create a list of programs and after opening the program MyTaskManager.exe, you will press the button "kill list processes." All of the process you add to the kill list will kill automatically. One thing I keep in mind with this program is that if there is some precious process, like system processes and maybe some other processes, you may not want to stop them at any cost. You must therefore add them to the except list. This process will never kill, although you add it in the kill list or attempt to kill it by mistake.

How It Works

It also reads the files Kill_List.xml and Except_List.xml. Keep the information while running the program. You can add a program to the kill list by right clicking on the process to add and then adding it to the list. The kill list is the list that the user will kill the processes with by clicking the button "kill list processes." The except list is the list of processes that you want to not kill at any cost, whether you add them to the kill list by accident or select for them for killing. It works like Task Manager, but I added some things that are following, through which you can get some extra work done.

Screenshot - Menue.jpg

After right clicking on the process, you can choose one of four operations for the selected process.

  1. You can open the folder containing the process.
  2. You can kill the selected process if it is not in the except list.
  3. You can add it to the except list.
  4. You can add it to the kill list.
Screenshot - Kill_button.jpgScreenshot - more_buttons.jpg

By clicking the kill list, it will kill all the processes that you've added to the kill list. View kill list and view except list to get data from the files Kill_List.xml and Except_List.xml and to view your processes in both categories. "New Task" just works like "Run" in the Windows menu.

Code Overview

In this program, I get all processes of the system through the function GetProcesses.

C++
string[] Mem_Usage = new string[100];
string[] ProcessNames = new string[100];
process = Process.GetProcesses();
noOfProcess = 0;
int i = 0;
ListViewItem lvi = new ListViewItem();
foreach (Process pro in process)
{
    String pathx = "";
    try
    {
    //get path of the procees 
        pathx = pro.Modules[0].FileName;
    //remove unnecessory special characters 
        pathx = pathx.Replace("\\??\\", "");
    // add whole path into string array
        path[i] = pathx.ToString();
    }
    Catch (Win32Exception)
    {
        path[i] = "";
    }
    // get the name of the process
    ProcessNames[i] = pro.ProcessName;
    // get the amount of ram using process
    Mem_Usage[i] = Convert.ToString(pro.PagedMemorySize64);
    // add process names into list view item
    lvi = new ListViewItem(ProcessNames[i]);
    // add momory usage by the process in to list item item as sub //item
    lvi.SubItems.Add(Mem_Usage[i]);
    // add list view item in to list view
    AddListViewItemsTolv(lvi);
    i++;
    // count numbers of process
    noOfProcess++;
}

After getting processes, we use one function, ReadXmlFile, for both the Except_List.xml and Kill_List.xml files.

public void ReadXmlFile(string filename)
{
    try
    {
        // name of the file to read
        reader = new XmlTextReader(filename);//,new 
        // it should handl whit space during reading
        reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
        // define the string Process in Kill list
        PInKill_List = new string[100];
        bool bEnableProcessNamesContent = false;
        bool bEnableProcessContent = false;
        int i = 0;
        // read upto end
        while (reader.Read())
        {
            // check the node of the reading data
            switch (reader.NodeType)
            {     
                //is it a Element of the node
                case System.Xml.XmlNodeType.Element:
                    bEnableProcessNamesContent = 
                       (reader.Name == "PorecessNames");
                    bEnableProcessContent = (reader.Name == "Process");
                    break;
                 // check for text in the node
                 case System.Xml.XmlNodeType.Text:
                     // add the value into text box
                     txtbox.Text += reader.Value + "\n";
                     PInKill_List[i] = reader.Value;
                     PInKill_List[i] = PInKill_List[i].TrimEnd('\r');
                     if (check)
                         noOfTageExist++;
                         i++;
                         break;
                }
            }
            check = false;
            reader.Close();
        } // end of try 
        catch (Exception)
        {
            reader.Close();
        }
    } 

}

I use the timer event to check processes after a specific time.

C++
public System.Timers.Timer timer = new System.Timers.Timer(4000);

The default time is 4 seconds, but the user can change the time of the refresh rate from 1 second up to 8 seconds. After the timer event call, I call the function Check4Process, which will compare the number of old processes with the number of new process. If it is the same, then leave it. If not, then the GetProcesss function will call again and add it to the list view control.

C++
private void Check4Processes()
{
    Process[] gPro = Process.GetProcesses();
    int Numpro = 0;
    string[] names = new string[100];
    string[] mem = new string[100];
    foreach (Process pro in gPro)
    {
        names[Numpro] = pro.ProcessName;
        mem[Numpro] = Convert.ToString(pro.PagedMemorySize64);
        Numpro++;
    }
    // if nuber of process are decrease 
    if (Numpro < noOfProcess)
    {
        //no of process are decreased
        GetProcesses();
        AddtoControl();
    }
    // if new process is added
    if (Numpro > noOfProcess)
    {
        // no of Process are increased
        GetProcesses();
        AddtoControl();
    }
}

In Kill_List, the function Check_Porcess_In_ExceptList is used when we are going to kill any process. We check whether it exists in the except list or not and return the bool value.

C++
public bool Check_Process_in_ExceptList(string toCheck, Kill_List Except_Obj)
{
    //SelectedProcess
    //Kill_List Except_Obj = new Kill_List("Except_List");
    Except_Obj.ReadXmlFile("Kill List.xml");
    Except_Obj.Show();
    string pNames = Except_Obj.txtbox.Text;
    bool found =false;
    string[] SingleProcessName = ConvertStringToStringArray(pNames);
    foreach(string str in SingleProcessName)
    {
        //found = (toCheck == str ? true : false);
        //System.Windows.Forms.MessageBox.Show(str);
        if(toCheck ==str)
        {
            found = true;
            break;
        }
    }
    return found;
}

History

  • 30 August, 2007 -- Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
I am working as Software Developer in http://www.abacus-global.com/

Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionResult does not match that from the system task manager Pin
LeoLiu_7-Jun-11 19:43
LeoLiu_7-Jun-11 19:43 
GeneralMy vote of 1 Pin
karim aslam5-Feb-11 22:45
karim aslam5-Feb-11 22:45 
GeneralQuestion: Task manager "Except" list C# function to VB.net Pin
jlkdaslkfjd11-Sep-10 22:17
jlkdaslkfjd11-Sep-10 22:17 
Questionrun... [modified] Pin
moncayos5-May-09 15:04
moncayos5-May-09 15:04 
AnswerRe: run... Pin
Naveed7276-May-09 17:30
Naveed7276-May-09 17:30 
Generaladvice Pin
y_12345678_913-Sep-08 6:47
y_12345678_913-Sep-08 6:47 
GeneralMy Task Manager Pin
ShowMeHow20-Jun-08 9:09
ShowMeHow20-Jun-08 9:09 
GeneralRe: My Task Manager Pin
EliottA2-Feb-09 9:19
EliottA2-Feb-09 9:19 
Generala comment by Miszou Pin
Sean Ewington4-Sep-07 3:36
staffSean Ewington4-Sep-07 3:36 
Questionhow to start demo Pin
dan o3-Sep-07 21:41
dan o3-Sep-07 21:41 
AnswerRe: how to start demo Pin
cdgwinn4-Sep-07 16:58
cdgwinn4-Sep-07 16:58 
GeneralRe: how to start demo Pin
dan o4-Sep-07 21:35
dan o4-Sep-07 21:35 
you are correct,
thank you

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.