ARTICLE

Diagnostics and Process in VB.NET: Part 1

Posted by Dinesh Beniwal Articles | Visual Basic Language August 28, 2010
In this article I will explain you about the Diagnostics and Process in VB.NET.
 
Reader Level:

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

table21.1.gif

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

table21.2.gif

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

process1.gif

Conclusion

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

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor