Here's my sample below, it prompts for a child process name and returns the parent process IDs and names for each instance of named process.
Get information about creating processes through process ID or process name
Using System; Using System.Collections.Generic; Using System.Text; Using System.Diagnostics; Namespace Testparentprocesscmd {class Program {static void Main (string[] args) {try {Console.WriteLine (' Please Enter t He name is the process you ' d like the parent for: " String procname = Console.ReadLine (); process[] proclist = Process.getprocessesbyname (procname); Get the parent process information through the process id//process proclist = Process.getprocessbyid (Convert.ToInt32 (procname)); ProcName = Proclist.processname; PerformanceCounter myprocid = new PerformanceCounter ("process", "ID process", procname); PerformanceCounter Myparentid = new PerformanceCounter ("process", "Creating Process ID", procname); float parentpid = Myparentid.nextvalue (); Console.WriteLine ("Parent for {0}: PID: {1} Name: {2}", ProcName, Parentpid, Process.getprocessbyid (int) parentpid). ProcessName); for (int i = 1; i < proclist.length i++) {PerformanceCounter mymultiprocid = new PerformanceCounter ("Process", "ID Pr Ocess ", ProcName +" # "+ i);PerformanceCounter myparentmultiprocid = new PerformanceCounter ("process", "Creating Process ID", ProcName + "#" + i); Parentpid = Myparentmultiprocid.nextvalue (); Console.WriteLine ("Parent for {0}#{1}: PID: {2} Name: {3}", ProcName, I, Parentpid, Process.getprocessbyid (int) PARENTPID). ProcessName); } catch (Exception ex) {Console.WriteLine ("Exception:" + ex.) message); finally {console.read ();}} } }
Original address:
Http://blogs.msdn.com/bclteam/archive/2006/06/20/640259.aspx
PerformanceCounter Constructors
Initializes a new instance of the PerformanceCounter class. Overloaded list
name |
Description |
PerformanceCounter () |
Initializes a new, read-only instance of the PerformanceCounter class, but does not associate the instance with any system performance counters or custom performance counters. |
PerformanceCounter (String, String) |
Initializes a new, read-only instance of the PerformanceCounter class and associates it with a specified system performance counter or a custom capability counter on the local computer. This constructor requires that the category contain a single instance. |
PerformanceCounter (String, String, Boolean) |
Initializes a new read-only or read/realistic example of the PerformanceCounter class and associates it with a specified system performance counter or a custom capability counter on the local computer. This constructor requires that the category contain a single instance. |
PerformanceCounter (String, String, String) |
Initializes a new, read-only instance of the PerformanceCounter class and associates it with a specified system performance counter or a custom property counter and class instance on the local computer. |
PerformanceCounter (String, String, String, Boolean) |
Initializes a new, read-only instance of the PerformanceCounter class, or a read/Realist example, and associates it with a specified system performance counter or a custom property counter and class instance on the local computer. |
PerformanceCounter (String, String, String, String) |
Initializes a new, read-only instance of the PerformanceCounter class and associates it with a specified system performance counter or a custom property counter and class instance on the specified computer. |
PerformanceCounter Basic Introduction and sample methods
A PerformanceCounter basic introduction
1 Simple Introduction
Represents the Windows NT performance counter Component
Namespaces: System.Diagnostics
Assembly: System (in System.dll)
2 constructors (for the purposes of this article only)
PerformanceCounter (String, String, String)
Function:
Initializes a new read-only instance of the PerformanceCounter class.
and associates it with a specified system performance counter or a custom property counter and class instance on the local computer
Parameter description:
Public PerformanceCounter (
String CategoryName,
String CounterName,
string InstanceName
)
CategoryName
The name of the performance counter category (performance object) associated with the performance counter.
CounterName
The name of the performance counter.
InstanceName
The name of the performance counter category instance, or an empty string ("") (If the category contains a single instance).
Two Sample methods:
Need to reference namespaces
Using System.Diagnostics;
Using System.Threading;
Using System.Collections;
1 getting a list of performance counter categories
Although there are many counter categories available in the system, the most frequently interacting with it may be cache, "Memory" (memory),
"Objects" (object)
, "PhysicalDisk" (Physical disks), "process" (processes), "Processor" (processors),
Categories of "server" (server), "System" and "thread" (threads)
public static void Getcategorynamelist ()
{
Performancecountercategory[] MYCAT2;
MYCAT2 = Performancecountercategory.getcategories ();
for (int i = 0; i < mycat2.length i + +)
{
Console.WriteLine (Mycat2[i]. Categoryname.tostring ());
}
}
2 Gets the name of the performance counter under the name instance of the instance under the performance counter category public static void Getinstancenamelistandcounternamelist (String Categorynam E
{
string [] instancenames;
ArrayList counters = new ArrayList ();
PerformanceCounterCategory mycat = new PerformanceCounterCategory (CategoryName);
Try
{
Instancenames = Mycat. Getinstancenames ();
if (instancenames.length = 0)
{
Counters. AddRange (Mycat. GetCounters ());
}
Else
{
for (int i = 0; i < instancenames.length i + +)
{
Counters. AddRange (Mycat. GetCounters (Instancenames[i]));
}
}
for (int i = 0; i < instancenames.length i + +)
{
Console.WriteLine (Instancenames[i]);
}
Console.WriteLine ("******************************");
foreach (PerformanceCounter counter in counters)
{
Console.WriteLine (counter. CounterName);
}
}
catch (Exception)
{
Console.WriteLine ("Unable to list of the counters for this category");
}
}
3 Displays the private static void Performancecounterfun (string Categor According to the Categoryname,countername,instancename performance situation) Yname, string instancename, String countername)
{
PerformanceCounter pc = new PerformanceCounter (CategoryName, CounterName, InstanceName);
while (true)
{
Thread.Sleep (1000); Wait for 1 second
float cpuload = pc. NextValue ();
Console.WriteLine ("CPU load =" + Cpuload + "%.");
}
}
4 Call Method 3 shows CPU usage performancecounterfun ("Processor", "_total", "% Processor time");