Operations on Your Computer Processes


This article is about the processes in your computer & operations on them.
 
You can use the Process class in the System.Diagnosis namespace.
 
using System.Diagnostics;
 

Using the Process.GetProcesses() method it will get all the processes in your computer as an array of Processes....
 
Process[] prc=Process.GetProcesses();
 
Every time it will first clear the ListBox otherwise it will append the processes to the ListBox. So we need to clear the ListBox before adding the processes in the ListBox.
 
lbProcess.Items.Clear();
 
Now I create one process called Load_Process & it will load all the processes currently running in the computer and display them in the ListBox.
 
 
private void Load_Process()
 {
     lbProcess.Items.Clear();
    
try
 
    {
         foreach (var item in prc)
         {
             lbProcess.Items.Add(item.ProcessName);
         }
     }  
     catch (Exception e)
     { 
         MessageBox.Show(e.Message.ToString());
     }
 }

 
See The following Image 

Load_Process.jpg

When the user clicks on the refresh button it will load all the process again into the ListBox.
 
 
private void btnRefresh_Click(object sender, EventArgs e)
 {
     Load_Process();
 }
 

After selecting a particular process it will kill that selected particular process from the list & then again it will load all the processes in the ListBox.
 
 
private void btnEndTask_Click(object sender, EventArgs e)
 {
     if (MessageBox.Show("Are You Sure Want To Kill..?", "Kill Process", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
     {
         prc[lbProcess.SelectedIndex].Kill();
         lbProcess.SelectedIndex = 0;
         Load_Process();
 }

 
See the Following Image

EndTask_Process.jpg

If the user wants to add a new task then by clicking the NewTask button it will start the new process & then it will load all the processes into ListBox, so that it will add that new process in the list.
 
 
private void btnNewTask_Click(object sender, EventArgs e)
 {
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.ShowDialog();
     Process.Start(ofd.FileName.ToString());
     Load_Process();
 }
 

See the Following Image :

NewTask_Process.jpg

After adding the new task in the list it will display in the ListBox process.
 
See the Following Image :


ListNewTask_Process.jpg

If the user wants to find a particular process from the list then it will find that specific process name from the list & if it will find then it will select that process in the ListBox.
 
You can find the process name as simple search using FindString method & find Exact match using FindStringExact method.
 
 
private void btnFind_Click(object sender, EventArgs e)
 {
     if (cbFindExact.Checked)
     {
         for (int i = 0; i < lbProcess.Items.Count; i++)
         {
             if (!(lbProcess.FindStringExact(txtFind.Text) == -1))
             {
                 lbProcess.SelectedIndex = lbProcess.FindStringExact(txtFind.Text);
                 break;
             }
            
else
 
            {
                 MessageBox.Show("No Process In List....");
                 break;
             }
         }
     }
    
else
 
    {
         for (int i = 0; i < lbProcess.Items.Count; i++)
         {
             if (!(lbProcess.FindString(txtFind.Text) == -1))
             {
                 lbProcess.SelectedIndex = lbProcess.FindString(txtFind.Text);
                 break;
             }
            
else
 
            {
                 MessageBox.Show("No Process In List....");
                 break;
             }
         }
     }
 }

 
See the following Image of Simple Search :


Find_Process.jpg

See the following Image Of Find Exact Match :

FindExact_Process.jpg

If the process does not find an exact match then it will display the error message to user.
 
See following Image :

NoFind_Process.jpg

This way you can perform all these operations of processes...


Similar Articles