Share via


How to: Investigate Thread Usage for Processes

You can view the process threads by reading the Threads property value of the Process component. The return value is of the type ProcessThreadCollection, which contains a collection of ProcessThread objects that represent the operating system threads currently running in the process. You can then iterate through the collection to view individual thread properties. The primary thread is not necessarily the thread at index 0 of the collection.

To investigate thread usage for a process

  1. If the process was not started by a Process component, associate a Process component to the desired process. For more information see How to: Bind to Existing Processes.

  2. Assign the Threads property value of the process to an empty collection variable of the type ProcessThread.

  3. Iterate through the array index to view properties for a single thread.

    The following example shows how to read the Threads property of Notepad and assign the value to an empty array. The BasePriority value of the first thread in the ProcessThread array is then read and displayed in a textbox named TextBox1.

    Dim myCollection As ProcessThreadCollection
    Dim myProcesses() As Process
    ' Create an instance of the Process Component and associate  
    ' it to the target process.
    myProcesses = Process.GetProcessesByName("Notepad.exe")
    ' Read the Process.Threads property and assign it to the empty array.
    myCollection = myProcesses(0).Threads
    ' Read desired ProcessThread property. 
    Me.Textbox1.Text = myCollection(0).BasePriority.ToString()
    
           ProcessThreadCollection threads;
            Process[] notepads;
            // Retrieve the Notepad processes.
            notepads = Process.GetProcessesByName("Notepad");
            // Read the Process.Threads property.
            threads = notepads[0].Threads;
            // Read desired ProcessThread property.
            TextBox1.Text = threads[0].BasePriority.ToString();
    

See Also

Tasks

How to: Bind to Existing Processes

How to: Create Process Components

Concepts

Introduction to Monitoring and Managing Windows Processes

Other Resources

Retrieving Information About Processes

Managing Processes