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 AppDomains, which are the managed equivalent of processes, is accomplished using the System and System.Threading namespaces.

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( ...

Get C# in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.