articles

Home / DeveloperSection / Articles / System Process in C#

System Process in C#

Sachindra Singh 5718 11-Feb-2011

What is System Process?

The Process component is useful to obtain the list of the processes that are running on the system.With the help of Process component tool we can manage   starting, stopping, controlling, and monitoring applications. The System.Diagnostics namespace contains functions that allow you to manage processes, threads, eventlogs and performance information. In this Demonstration I am trying to show how we can obtain list of process that are currently open and close.

Code:
class Demonstration_Of_Process
    {
        public voidCheck(Process[] pr)//Creating check function with parameter
        {
            int flag=0;//creating int variable initializing from 0
            Process[] current=pr;//creating array(current) of process and initializing from pr variable that contain list of processing that are running
            Process[] previous=pr;//creating array(previous) of process and initializing from pr variable that contain list of processing that are running
            while (true)//while loop will be execute inifinte time to obtain list of process that are running on the system
            {
                current=Process.GetProcesses();//current variable containing the list of the application that are running
                if (current.Length> previous.Length)//checking condition current length of process is greater than previous length of process or not
                {
                    foreach (Process cur incurrent)//loop executing
                    {
                        if (flag==0)//checking condition
                        {
                            foreach (Processtemp inprevious)//loop executing
                            {
                                if (temp.ProcessName==cur.ProcessName)// condition checking that application is runing or not
                                {
                                    flag =1;//variable flag initializing from 1
                                    break;// current loop will be terminate if application exist in the process     
                                }
                           }                       
                        }
                        if (flag==0)//checking condition
                        {
                            Console.WriteLine(" Start-->"+cur.ProcessName);//message will be show on command window
                        }
                        flag =0;//flag initializing from 0
                    }
                }
                else
                {
                    if (current.Length< previous.Length)//checking condition if any application has closed
                    {
                        foreach (Processcurinprevious)//loop executing of pervious process
                        {
                            foreach (Processtemp incurrent)//loop executing of current process
                                {
                                    if (temp.ProcessName==cur.ProcessName)//condition checking  that application which application has terminated
                                    {
                                        flag =1;//variable flag initializing from 1
                                        break;//current loop will be terminate if application exist in the process        
                                    }
                                }
 
                                if (flag==0)//checking condition
                            {
                                Console.WriteLine(" Close -->"+cur.ProcessName);//message will be show on command window
                            }
                            flag =0;//flag initializing from 0
                        }
                    }
                }
                previous=current;//copying all list of process in previous variable
            }
        }
        publicstaticvoidMain()
        {
            Demonstration_Of_Processdof= new Demonstration_Of_Process();//creating object of the class
            Process[] pp= Process.GetProcesses();//creating process array and store list of application in pp variable from GetProcesses() method
            dof.Check(pp);//calling parametrize function and passing value pp
        }
    }

After run this Demonstration if I open Notepad application the window will display message “Start->notepad”  it means Notepad has open as shown below:

Output

System Process in C#

If I close Notepad, the command window will show message “Close notepad”  it means application has closed as shown below:

Output

System Process in C#



Updated 04-Mar-2020

Leave Comment

Comments

Liked By