Diagnostics
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
Imports System
Imports
System.Diagnostics
Namespace
DiagnosticsProcess
Class Program
Shared Sub Main(ByVal
args As String())
Dim
CurrentProcess As
Process = Process.GetCurrentProcess()
Console.WriteLine("ProcessName:
{0}", CurrentProcess.ProcessName)
End Sub
End Class
End Namespace
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
Imports System
Imports
System.Diagnostics
Namespace
DiagnosticsProcess
Class Program
Shared Sub Main(ByVal
args As String())
Dim procList
As Process()
= Process.GetProcesses()
' or you can specify a
computer name
' Process[]
procList = Process.GetProcesses(@"MCBcomputer");
' output the
first 20 processes in the array
Dim i
As Integer = 0
While i <
20
Console.WriteLine("ProcessID:
{0}", procList(i).Id)
Console.WriteLine("ProcessName:
{0}", procList(i).ProcessName)
System.Math.Max(System.Threading.Interlocked.Increment(i),
i - 1)
End While
End Sub
End Class
End Namespace
Output

Conclusion
Hope this
article would have helped you in understanding the Diagnostics and Process in
VB.NET.