VB.NET Diagnostics and Process: Part 2

In this article I will explain you about the Diagnostics and Process in VB.NET.
  • 1923

As you read Process in previous article now this is reaming part. 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 Not process.CloseMainWindow() Then
        ' you can Kill process with Kill method if you cannot close it
           process.Kill()
    End If

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 

    Imports System
    Imports System.Diagnostics
    Imports System.Timers
    Namespace DiagnosticsProcess
        Class Program
            Shared Sub Main(ByVal args As String())
                Dim CurrentProcess As Process = Process.GetCurrentProcess()
                Dim threadList As ProcessThreadCollection = CurrentProcess.Threads
                Console.WriteLine("ProcessName: {0}", CurrentProcess.ProcessName)
                For Each thr As ProcessThread 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)
                Next
            End Sub
        End 
Class
    End Namespace

Output

process2.gif
 

Conclusion

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.