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.
After right clicking on the process, you can choose one of four operations for the selected process.
- You can open the folder containing the process.
- You can kill the selected process if it is not in the except list.
- You can add it to the except list.
- You can add it to the kill list.
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
.
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
{
pathx = pro.Modules[0].FileName;
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
{
reader = new XmlTextReader(filename);
reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
PInKill_List = new string[100];
bool bEnableProcessNamesContent = false;
bool bEnableProcessContent = false;
int i = 0;
while (reader.Read())
{
switch (reader.NodeType)
{
case System.Xml.XmlNodeType.Element:
bEnableProcessNamesContent =
(reader.Name == "PorecessNames");
bEnableProcessContent = (reader.Name == "Process");
break;
case System.Xml.XmlNodeType.Text:
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();
}
catch (Exception)
{
reader.Close();
}
}
}
I use the timer event to check processes after a specific time.
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.
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 (Numpro < noOfProcess)
{
GetProcesses();
AddtoControl();
}
if (Numpro > noOfProcess)
{
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.
public bool Check_Process_in_ExceptList(string toCheck, Kill_List Except_Obj)
{
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)
{
if(toCheck ==str)
{
found = true;
break;
}
}
return found;
}
History
- 30 August, 2007 -- Original version posted