Home > DeveloperSection > Articles > RUNNING PROCESS IN C#

RUNNING PROCESS IN C#


C# C# 
Ratings:
0 Comment(s)
 7452  View(s)
Rate this:

RUNNING PROCESS IN C#

In this project we are checking number of process which is running inside the task manager and also we are showing how to kill Selected Process from the ListBox with the help of Remove Process Button as shown below:

RUNNING PROCESS IN C#

First we have to take Listbox, two Button as a control in the form, By clicking on Button Listbox appear number of Process running currently, and if we select one process inside the ListBox we will kill the Process.

In this Project we are are killing “notepad” application with the help of “Click Process Button” and it will appear a message like “Deleted Process”.

The code Snippet describes as below:

On the Click Event of the button we have to write a following code.

private void btnProcess_Click(object sender, EventArgs e)

        {           

            try                

            {              

                RunningProcess.Items.Clear();//Clear all the Process which Running already.

                Process[] p = Process.GetProcesses();//Number of Process which is running inside has been taken inside the array.

                for (int i = 0; i < p.Length; i++)

                {

                    //Adding all the running Process inside array

                    RunningProcess.Items.Add(p[i].ProcessName);

//checking Process as a “notepad”.

                    if (p[i].ProcessName == "notepad")

                    {

                      //Killing Process

                        p[i].Kill();

                        MessageBox.Show("successfull killed");//messagebox shows Successfull killed after killing the Notepad process

                    }

 

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

        }

 

        private void btnRemove_Click(object sender, EventArgs e)

        {

            while (RunningProcess.SelectedItems.Count > 0)

            {

//This shows that Number of Running Process inside the LabelBox if we choose or select the process inside the labelbox and Kill it with the help of Remove Process Button.

             RunningProcess.Items.Remove(RunningProcess.SelectedItem);

              

           }

        }

}

The Desired Output of the Code look likes.

You will see in the output Snapshot as soon as we click “Click to Check Running Process” Button various Processes is running inside the machine will be shown in the labelbox as you can see below.

RUNNING PROCESS IN C#

Button to show Running process

Now u can see inside the Taskbar Notepad is running as u Click again on the Button “Notepad Process will be killed. If you want to kill Process you can select in the Listbox and Click on the Remove Button that process is no more active.

RUNNING PROCESS IN C#


Don't want to miss updates? Please click the below button!

Follow MindStick