19.4 Processes, Threads, and Stacks

The .NET Framework provides managed execution of code. However, managed applications live alongside unmanaged applications, and need to coexist. It can be useful for a managed application to have access to information about the atoms of unmanaged execution, namely operating system processes and threads. Additionally, since "managed execution" implies the existence of some overarching facility monitoring the execution process itself, it is not unreasonable to wish for access to detailed information about the execution process. Both of these needs are met by the classes in the System.Diagnostics namespace, providing access to unmanaged processes and threads, as well as access to managed stack frames . Access to managed threads and AppDomain s, which are the managed equivalent of processes, is accomplished using the System and System.Threading namespaces.

19.4.1 Launching a New Process

The Process class can be used to launch new operating system processes, enumerate and kill existing ones, and monitor the vital statistics of a running process. The Process.Start method has overloads that range from taking the filename of an EXE to launch to taking a populated ProcessStartInfo instance, which fully specifies the parameters for process launching. The latter approach can also be used to capture and redirect the launched process's stdin , stdout and stderr , as the following sample demonstrates :

 public void LaunchDirCommand( ) {   ProcessStartInfo psi = new ProcessStartInfo( );   psi.FileName = "cmd.exe";   psi.Arguments = "/c dir";   psi.RedirectStandardOutput = true;   psi.UseShellExecute = false;   Process p = Process.Start(psi);   StreamReader stm = p.StandardOutput;   string s = stm.ReadToEnd( );   Console.WriteLine(s); } 

19.4.2 Examining Running Processes

The Process.GetProcess XXX methods allow an application to retrieve a specific process by name or process ID, and to enumerate all running processes. Given a valid process instance, a wealth of properties provide access to the process's vital statistics such as name , ID, priority, memory and processor utilization, window handles, etc. The following sample enumerates some basic information for all the running processes in the system:

 public void EnumerateRunningProcesses( ) {   Process[ ] procs = Process.GetProcesses( );   foreach (Process p in procs) {     Console.WriteLine("{0} ({1})", p.ProcessName, p.Id);     Console.WriteLine("  Started: {0}", p.StartTime);     Console.WriteLine("  Working set: {0}", p.WorkingSet);     Console.WriteLine("  Processor time: {0}", p.TotalProcessorTime);     Console.WriteLine("  Threads: {0}", p.Threads.Count);   } } 

19.4.3 Examining Threads in a Process

Building on the previous example, it is also possible to retrieve information on the operating system threads in a process using the Process.Threads property. This returns a collection of ProcessThread instances, each representing an underlying operating system thread (which may or may not have a managed System.Threading.Thread counterpart ). Given a reference to a ProcessThread instance we can discover a host of information about the underlying thread and even control aspects such as thread priority and processor affinity.

 public void EnumerateThreads(Process p) {   ProcessThreadCollection ptc = p.Threads;   foreach (ProcessThread pt in ptc) {     Console.WriteLine("Thread {0} ({1})", pt.Id, pt.ThreadState);     Console.WriteLine("  Priority Level: {0}", pt.PriorityLevel);     Console.WriteLine("  Started: {0}", pt.StartTime);     Console.WriteLine("  Processor time: {0}", pt.TotalProcessorTime);   }     } ... Process p = Process.GetCurrentProcess( ); EnumerateThreads(p); 

19.4.4 Examining Stack Frames for a Thread

One of the benefits of managed execution is that the Execution Engine has visibility into the execution process for managed applications. The StackTrace and StackFrame classes provide read-only access to some of this information, namely the application call stack and the individual stack frames. This information can be used in exception handling code to determine what led up to the failure, and to improve the quality of error reporting.

 // Compile with /debug+ to disable JIT inlining, provide src debug info using System; using System.Diagnostics;    class DumpStackFrames {   static void WalkStack( ) {     StackTrace st = new StackTrace(true); // true=  =use .PDB if available     Console.Write("Current stack has {0} frames", st.FrameCount);     for (int i=0; i<st.FrameCount; i++) {       StackFrame sf = st.GetFrame(i);       Console.Write("\nFrame {0}: {1}, ", i, sf.GetMethod( ).Name);       Console.Write("{0}@{1}", sf.GetFileName( ), sf.GetFileLineNumber( ));     }   }   static void A( ) { B( ); }   static void B( ) { C( ); }   static void C( ) { WalkStack( ); }   static void Main( ) { A( ); } } 

A run of the preceding example produces the following output:

 Current stack has 5 frames Frame 0: WalkStack, c:\CSiaN\Diagnostics\DumpStack.cs@7 Frame 1: C, c:\CSiaN\Diagnostics\DumpStack.cs@17 Frame 2: B, c:\CSiaN\Diagnostics\DumpStack.cs@16 Frame 3: A, c:\CSiaN\Diagnostics\DumpStack.cs@15 Frame 4: Main, c:\CSiaN\Diagnostics\DumpStack.cs@18 


C# in a Nutshell
C # in a Nutshell, Second Edition
ISBN: 0596005261
EAN: 2147483647
Year: 2005
Pages: 963

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net