日期:2008-05-05  浏览次数:20502 次

A process is an instance of a running application on a system. A process itself is nothing besides memory address space. Besides the space, a process also owns resources including files, dynamic and virtual memory allocations, and threads. Each process has a unique ID. For a process to accomplish anything, it must own thread(s). Each process has at least one main thread, and can have any number of secondary threads, also known as worker threads.
A thread is a path of execution that executes some part of code in a predefined manner. A thread executes this code in a processes address space. Each thread has a unique ID, and allocates its own resources such as CPU registers and stack.
In this article, we will discuss how to return all the available processes on a machine, and all the threads corresponding to a process. The sample application we develop shows the total number of running processes, their IDs, and corresponding information such as physical and virtual memory, starting time, etc. This program also lets us monitor available threads, information that is not available in Windows Task Manager.
System.Diagnostics Namespace
In the .NET Framework, the System.Diagnostics namespace defines classes that allow us to debug and trace applications, read and write event logs, and monitor processes and system performance using performance counters. The major classes include Debug, Counter, EventLog, Process, ProcessThread, StackTrace, and Trace.
A discussion of all these classes is out of the scope of this article - we will cover only the Process class and its related classes.
The Process Class
In .NET, the Process class enables us to start and stop processes on a local or a remote machine. The Process class also enables us to monitor system resources, such as all running processes, their ID, threads corresponding to process IDs, and threads related to each process. It also lets us monitor resources allocated by processes and threads, such as physical memory, paged memory, virtual memory, peak paged memory, and peaked virtual memory size.
Creating an instance and get all running Processes
There are two ways to create an instance of a Process class. Either we call the Process constructor followed by the Start method to start a process, or we can directly call the GetProcesses method, which returns all the running processes on a machine. The Process class constructor doesn't take any parameters. See the following definition of the Process constructor.
Using System.Diagnostics;

public Process();

Process prCSS = new Process();
prCSS.Start();
OR:
Process[] procs = Process.GetProcesses();
Get all the running processes
The GetProcesses method of the Process class returns all the running processes on a machine. The following code returns all the processes and writes to the system console.
Process[] procs = Process.GetProcesses();
foreach(Process proc in procs)
{
   Console.Write("Process Name:"+proc.ProcessName +",");
}
Get Process Names and Process IDs
We use the Process Name and Id properties to get process name and process IDs of the processes:
Process[] procs = Process.GetProcesses();
foreach(Process proc in procs)
{
   Console.Write("Process Name:"+proc.ProcessName +",");
   Console.Write("Process Id:"+ proc.Id.ToString()+",");
}
Get Process Resources Information
By using the Process class we can even discover resource data such as physical and virtual memory for a process. The WorkingSet property returns the physical memory and VirtualMemorySize re