📄 form1.cs
字号:
namespace Ch12_6
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
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()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
sProcIds = new ArrayList();
// First, fill in the machine name
machinename.Text = Process.GetCurrentProcess().MachineName;
// Now, load the processes
Process[] sProcess = Process.GetProcesses();
foreach( Process p in sProcess )
{
listBox1.Items.Add( p.ProcessName );
sProcIds.Add( p.Id );
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose()
{
base.Dispose();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
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();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Machine name:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Running Processes:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 184);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 3;
this.label3.Text = "Threads:";
//
// machinename
//
this.machinename.Location = new System.Drawing.Point(128, 16);
this.machinename.Name = "machinename";
this.machinename.Size = new System.Drawing.Size(100, 16);
this.machinename.TabIndex = 6;
//
// ThreadPriority
//
this.ThreadPriority.Text = "Priority";
//
// ThreadID
//
this.ThreadID.Text = "Thread ID";
//
// VirtualMemory
//
this.VirtualMemory.Text = "Virtual Mem";
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(16, 80);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(408, 95);
this.listBox1.TabIndex = 2;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.SelectItemHandler);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.ThreadID,
this.ThreadPriority,
this.ProcessorTime});
this.listView1.Location = new System.Drawing.Point(16, 200);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(408, 97);
this.listView1.TabIndex = 7;
//
// ProcessorTime
//
this.ProcessorTime.Text = "Processor Time";
this.ProcessorTime.Width = 90;
//
// Priority
//
this.Priority.Text = "Priority";
//
// Id
//
this.Id.Text = "ID";
this.Id.Width = 20;
//
// button1
//
this.button1.Location = new System.Drawing.Point(352, 312);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 5;
this.button1.Text = "Close";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(440, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listView1,
this.machinename,
this.button1,
this.label3,
this.listBox1,
this.label2,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
protected void SelectItemHandler (object sender, System.EventArgs e)
{
int idx = this.listBox1.SelectedIndex;
// Get the threads for this process id
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();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args)
{
Application.Run(new Form1());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -