Listing all threads for a process inn a ListView : Process « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » ProcessScreenshots 
Listing all threads for a process inn a ListView
  


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.ColumnHeader VirtualMemory;
    private System.Windows.Forms.ColumnHeader Priority;
    private System.Windows.Forms.ColumnHeader Id;
    private System.Windows.Forms.Label machinename;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ListView listView1;
    private System.Windows.Forms.ColumnHeader ThreadID;
    private System.Windows.Forms.ColumnHeader ThreadPriority;
    private System.Windows.Forms.ColumnHeader ProcessorTime;
    private ArrayList sProcIds;

    public Form1() {
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.machinename = new System.Windows.Forms.Label();
        this.ThreadPriority = new System.Windows.Forms.ColumnHeader();
        this.ThreadID = new System.Windows.Forms.ColumnHeader();
        this.VirtualMemory = new System.Windows.Forms.ColumnHeader();
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.listView1 = new System.Windows.Forms.ListView();
        this.ProcessorTime = new System.Windows.Forms.ColumnHeader();
        this.Priority = new System.Windows.Forms.ColumnHeader();
        this.Id = new System.Windows.Forms.ColumnHeader();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(1616);
        this.label1.Size = new System.Drawing.Size(8816);
        this.label1.Text = "Machine name:";
        this.label2.Location = new System.Drawing.Point(1648);
        this.label2.Size = new System.Drawing.Size(10016);
        this.label2.Text = "Running Processes:";
        this.label3.Location = new System.Drawing.Point(16184);
        this.label3.Size = new System.Drawing.Size(10016);
        this.label3.Text = "Threads:";
        this.machinename.Location = new System.Drawing.Point(12816);
        this.machinename.Size = new System.Drawing.Size(10016);
        this.ThreadPriority.Text = "Priority";
        this.ThreadID.Text = "Thread ID";
        this.VirtualMemory.Text = "Virtual Mem";
        this.listBox1.Location = new System.Drawing.Point(1680);
        this.listBox1.Size = new System.Drawing.Size(40895);
        this.listBox1.SelectedIndexChanged += new
           System.EventHandler(this.SelectItemHandler);
        this.listView1.Columns.AddRange(new
           System.Windows.Forms.ColumnHeader[] {
          this.ThreadID,
          this.ThreadPriority,
          this.ProcessorTime});
        this.listView1.Location = new System.Drawing.Point(16200);
        this.listView1.Size = new System.Drawing.Size(40897);
        this.ProcessorTime.Text = "Processor Time";
        this.ProcessorTime.Width = 90;
        this.Priority.Text = "Priority";
        this.Id.Text = "ID";
        this.Id.Width = 20;
        this.button1.Location = new System.Drawing.Point(352312);
        this.button1.Size = new System.Drawing.Size(6424);
        this.button1.Text = "Close";
        this.button1.Click += new
             System.EventHandler(this.button1_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(440349);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                     this.listView1,
                     this.machinename,
                     this.button1,
                     this.label3,
                     this.listBox1,
                     this.label2,
                     this.label1});
        this.ResumeLayout(false);
        sProcIds = new ArrayList();
        machinename.Text = Process.GetCurrentProcess().MachineName;

        Process[] sProcess = Process.GetProcesses();
        foreach (Process p in sProcess) {
            listBox1.Items.Add(p.ProcessName);
            sProcIds.Add(p.Id);
        }
    }

    protected void SelectItemHandler(object sender, System.EventArgs e) {
        int idx = this.listBox1.SelectedIndex;
        Process proc = Process.GetProcessById((int)sProcIds[idx]);
        ProcessThreadCollection procThreads = proc.Threads;
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.Items.Clear();
        int nRow = 0;
        foreach (ProcessThread pt in procThreads) {
            string priority = "Normal";
            switch ((int)proc.BasePriority) {
                case 8:
                    priority = "Normal";
                    break;
                case 13:
                    priority = "High";
                    break;
                case 24:
                    priority = "Real Time";
                    break;
                case 4:
                default:
                    priority = "Idle";
                    break;
            }
            this.listView1.Items.Add(pt.Id.ToString());
            this.listView1.Items[nRow].SubItems.Add(priority);
            this.listView1.Items[nRow].SubItems.Add(pt.UserProcessorTime.ToString());
            nRow++;
        }
    }

    protected void button1_Click(object sender, System.EventArgs e) {
        Close();
    }
    public static void Main(string[] args) {
        Application.Run(new Form1());
    }
}

   
  
Related examples in the same category
1.Get current Process Name
2.Start And Kill Process
3.Running another program from your own.
4.CloseMainWindow,WaitForExit
5.Enum Modules For Pid
6.Build up a list of the running processes
7.Input Output:Starting ProcessesInput Output:Starting Processes
8.Redirecting Process OutputRedirecting Process Output
9.Detecting Process CompletionDetecting Process Completion
10.Get Process propertyGet Process property
11.Get ThreadsGet Threads
12.List ProcessList Process
13.List ThreadsList Threads
14.Start Process With File name
15.Create Process As User
w___w_w.ja_v__a_2___s___._co__m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.