This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.
The System.Diagnostics namespace provides classes that enable you to debug and trace code; start, stop, and kill processes; monitor system performance; and read and write event logs. Table 21.1 describes some System.Diagnostics namespace classes.
Table 21.1: System.Diagnostics Classes
The Process Class
The Process class retrieves various information pertaining to a process running on the system and caches the information in memory. This means that if your application requires up-to-date process information, you should frequently refresh the process cache by calling the Process.Refresh() method. In addition to simply retrieving the name of the current process, this class can provide more information, such as the following:
- Memory statistics
- Process times
- Thread statistics
- Handle statistics
Table 21.2 describes the members of this class.
Table 21.2: Process Class Members
Listing 21.1 illustrates how to get the current process from the Process class. In this case, the process
would be the application name you gave to the following code(Listing 21-1) after the code is compiled into an assembly.
Listing 21.1: Getting a Current Process
using System;
using System.Diagnostics;
namespace DiagnosticsProcess
{
class Program
{
static void Main(string[] args)
{
Process CurrentProcess = Process.GetCurrentProcess();
Console.WriteLine("ProcessName: {0}", CurrentProcess.ProcessName);
}
}
}
Although you plan to enumerate processes currently running on the system, you may not be authorized to access process-specific information. If any of the Process class methods fail (usually with ACCESS_DENIED), that may throw an exception corresponding to the failure. For example:
System.ComponentModel.Win32Exception: Access is denied
It has a method named GetProcesses() that returns all running processes on the machine. The GetProcesses has two overloads: one for the local machine and another for the remote machine. To see processes on the local machine, use GetProcesses(); otherwise use GetProcesses(string machinename). Listing 21.2 shows how to list the first 20 running processes on your computer. You may opt to loop or enumerate through all the processes from the returned array, but here we just list 20.
Listing 21.2: Using GetProcess
using System;
using System.Diagnostics;
namespace DiagnosticsProcess
{
class Program
{
static void Main(string[] args)
{
Process[] procList = Process.GetProcesses();
// or you can specify a computer name
// Process[] procList = Process.GetProcesses(@"MCBcomputer");
// output the first 20 processes in the array
for (int i = 0; i < 20; i++)
{
Console.WriteLine("ProcessID: {0}", procList[i].Id);
Console.WriteLine("ProcessName: {0}", procList[i].ProcessName);
}
}
}
}
The Process class offers properties from which you can learn things about a process. You can also start a process, close it, and kill it if you have the right authorization. Listing 21.3 depicts some of the important process methods with inline comments.
Listing 21.3: Various Process Methods
// you can start process with Start method
process.Start();
// wait until the application is in an idle message loop state indefinitely
process.WaitForInputIdle();
// wait until the application is in an idle message loop state 500miliseconds
// process.WaitForInputIdle(500);
// try to close the application main window GUI.
// you cannot close processes on remote computers!
// use Close() for console and GUI-based applications!
// but always prefer using CloseMainWindow() for GUI apps!
if (!process.CloseMainWindow())
{
// you can Kill process with Kill method if you cannot close it
process.Kill();
}
// wait for the application to exit indefinitely or 500miliseconds
// process.WaitForExit();
// process.WaitForExit(500);
The EnterDebugMode method of the Process class puts the process in the debug state, and the LeaveDebugMode method forces the process to exit the debug state and return to the normal state.
Process Threads
A process consists of at least one thread, the primary execution thread, which can spawn more needed threads. You should limit the total number of threads because synchronization and contextswitching operations can drain the performance of the thread actions. The number of threads you allow depends on your resources and code. Thus, at the outset, we recommend you limit the maximum concurrent threads to a modest number, but do leave the maximum number of threads option of your process as configurable via the registry or other means such as the application *.ini file. The application consumer can monitor the default application performance and then reduce or augment the maximum number of threads if fine-tuning is necessary.
All process threads share the address space of the primary execution thread. A thread is made up of code, data, and other resources such as open files, semaphores, and dynamically allocated memory. Process threads share process resources. Each thread has a private thread local storage (TLS) area for its memory operations. Threads run in various intervals inside a process according to priorities assigned by the code and operating system. An atomic system scheduler gives execution control to threads in a round-robin fashion. The threads are run in clockwise order, each thread running in a time slice inside a virtual ring. The system scheduler determines which threads should run and when, so you do not need to worry about this aspect of thread execution. However, threads with lower priority use less CPU cycles to execute their code than higher-priority threads. To balance the CPU load on systems with multiple CPUs, the system scheduler may opt to move some threads to other CPUs. Listing 21.4 shows how you can list the running processes and their spawned threads on your system.
Listing 21.4: Listing Threads
using System;
using System.Diagnostics;
using System.Timers;
namespace DiagnosticsProcess
{
class Program
{
static void Main(string[] args)
{
Process CurrentProcess = Process.GetCurrentProcess();
ProcessThreadCollection threadList = CurrentProcess.Threads;
Console.WriteLine("ProcessName: {0}", CurrentProcess.ProcessName);
foreach (ProcessThread thr in threadList)
{
Console.WriteLine("Thread ID: {0}", thr.Id);
Console.WriteLine("Thread Priority Level: {0}", thr.PriorityLevel);
Console.WriteLine("Thread Total Processor Time: {0}",
thr.TotalProcessorTime);
}
}
}
}
Conclusion
Hope this article would have helped you in understanding the Diagnostics and Process in C#. See other articles on the website on .NET and C#.
|
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer. |