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 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.
starting, stopping, controlling, and monitoring applications.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 (Process temp 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 (Process temp 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
}
} public staticvoidMain()
{Demonstration_Of_Process dof= 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
If I close Notepad, the command window will show message “Closeànotepad” it means application has closed as shown below:
Leave Comment
1 Comments
View All Comments