📄 form2.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
namespace HyperBarServer
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblCntName;
private System.Windows.Forms.ListBox lbCounters;
private System.Windows.Forms.Label lblInstName;
private System.Windows.Forms.ListBox lbInstances;
private System.Windows.Forms.Label lblCatName;
private System.Windows.Forms.ListBox lbCategories;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Label lblSelectedCounter1;
private System.Windows.Forms.Label lblSelectedCounter;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblCntName = new System.Windows.Forms.Label();
this.lbCounters = new System.Windows.Forms.ListBox();
this.lblInstName = new System.Windows.Forms.Label();
this.lbInstances = new System.Windows.Forms.ListBox();
this.lblCatName = new System.Windows.Forms.Label();
this.lbCategories = new System.Windows.Forms.ListBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.lblSelectedCounter1 = new System.Windows.Forms.Label();
this.lblSelectedCounter = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblCntName
//
this.lblCntName.Location = new System.Drawing.Point(392, 20);
this.lblCntName.Name = "lblCntName";
this.lblCntName.Size = new System.Drawing.Size(88, 16);
this.lblCntName.TabIndex = 4;
this.lblCntName.Text = "计数器名称:";
//
// lbCounters
//
this.lbCounters.HorizontalScrollbar = true;
this.lbCounters.ItemHeight = 12;
this.lbCounters.Location = new System.Drawing.Point(392, 40);
this.lbCounters.Name = "lbCounters";
this.lbCounters.Size = new System.Drawing.Size(168, 256);
this.lbCounters.Sorted = true;
this.lbCounters.TabIndex = 5;
this.lbCounters.SelectedIndexChanged += new System.EventHandler(this.lbCounters_SelectedIndexChanged);
//
// lblInstName
//
this.lblInstName.Location = new System.Drawing.Point(208, 20);
this.lblInstName.Name = "lblInstName";
this.lblInstName.Size = new System.Drawing.Size(72, 16);
this.lblInstName.TabIndex = 2;
this.lblInstName.Text = "实例名称:";
//
// lbInstances
//
this.lbInstances.HorizontalScrollbar = true;
this.lbInstances.ItemHeight = 12;
this.lbInstances.Location = new System.Drawing.Point(208, 40);
this.lbInstances.Name = "lbInstances";
this.lbInstances.Size = new System.Drawing.Size(168, 256);
this.lbInstances.Sorted = true;
this.lbInstances.TabIndex = 3;
this.lbInstances.SelectedIndexChanged += new System.EventHandler(this.lbInstances_SelectedIndexChanged);
//
// lblCatName
//
this.lblCatName.Location = new System.Drawing.Point(24, 20);
this.lblCatName.Name = "lblCatName";
this.lblCatName.Size = new System.Drawing.Size(80, 16);
this.lblCatName.TabIndex = 0;
this.lblCatName.Text = "类别名称:";
//
// lbCategories
//
this.lbCategories.HorizontalScrollbar = true;
this.lbCategories.ItemHeight = 12;
this.lbCategories.Location = new System.Drawing.Point(24, 40);
this.lbCategories.Name = "lbCategories";
this.lbCategories.Size = new System.Drawing.Size(168, 256);
this.lbCategories.Sorted = true;
this.lbCategories.TabIndex = 1;
this.lbCategories.SelectedIndexChanged += new System.EventHandler(this.lbCategories_SelectedIndexChanged);
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(408, 344);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 24);
this.btnOK.TabIndex = 8;
this.btnOK.Text = "确定(&O)";
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(488, 344);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(72, 24);
this.btnCancel.TabIndex = 9;
this.btnCancel.Text = "取消(&C)";
//
// lblSelectedCounter1
//
this.lblSelectedCounter1.Location = new System.Drawing.Point(24, 310);
this.lblSelectedCounter1.Name = "lblSelectedCounter1";
this.lblSelectedCounter1.Size = new System.Drawing.Size(80, 16);
this.lblSelectedCounter1.TabIndex = 6;
this.lblSelectedCounter1.Text = "所选计数器:";
//
// lblSelectedCounter
//
this.lblSelectedCounter.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblSelectedCounter.Location = new System.Drawing.Point(24, 328);
this.lblSelectedCounter.Name = "lblSelectedCounter";
this.lblSelectedCounter.Size = new System.Drawing.Size(352, 40);
this.lblSelectedCounter.TabIndex = 7;
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 390);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblSelectedCounter1,
this.lblSelectedCounter,
this.btnCancel,
this.btnOK,
this.lblCntName,
this.lbCounters,
this.lblInstName,
this.lbInstances,
this.lblCatName,
this.lbCategories});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form2";
this.Text = "选择计数器";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
private string sCatName;
private string sInstName;
private string sCntName;
public string CategoryName
{
get
{
return sCatName;
}
}
public string InstanceName
{
get
{
return sInstName;
}
}
public string CounterName
{
get
{
return sCntName;
}
}
private void Form2_Load(object sender, System.EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
try
{
ReloadPerformanceCounters();
}
catch (Exception ex)
{
MessageBox.Show("无法枚举性能计数器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.DialogResult = DialogResult.Cancel;
}
this.Cursor = Cursors.Default;
}
private void ReloadPerformanceCounters()
{
lbCategories.Items.Clear();
lbInstances.Items.Clear();
lbCounters.Items.Clear();
sCatName = "";
sInstName = "";
sCntName = "";
lblSelectedCounter.Text = Form1.BuildCounterString(sCatName, sInstName, sCntName);
// Get all the counter categories from the local machine
PerformanceCounterCategory[] pccs = PerformanceCounterCategory.GetCategories();
// Fill performance counter category list
for (int i = 0; i < pccs.Length; i++)
{
lbCategories.Items.Add(pccs[i].CategoryName);
}
// Select first item by default
lbCategories.SelectedIndex = 0;
lbCategories.Enabled = true;
}
private void lbCategories_SelectedIndexChanged(object sender, System.EventArgs e)
{
PerformanceCounterCategory pcc = new PerformanceCounterCategory(lbCategories.SelectedItem.ToString());
string[] InstNames = null;
bool bInstExists;
lbInstances.Items.Clear();
lbCounters.Items.Clear();
// Check instance existance of the category
try
{
InstNames = pcc.GetInstanceNames();
bInstExists = true;
}
catch (InvalidOperationException ex)
{
bInstExists = false;
}
if (!bInstExists)
{
lbInstances.Items.Add("<没有实例>");
lbCounters.Items.Add("<没有计数器>");
lbInstances.Enabled = false;
lbCounters.Enabled = false;
sCatName = "";
sInstName = "";
sCntName = "";
lblSelectedCounter.Text = Form1.BuildCounterString(sCatName, sInstName, sCntName);
}
else
{
if (InstNames.Length > 0)
{
for (int i = 0; i < InstNames.Length; i++)
{
lbInstances.Items.Add(InstNames[i]);
}
// Select first item by default
lbInstances.SelectedIndex = 0;
lbInstances.Enabled = true;
}
else
{
// default instance
lbInstances.Items.Add("<默认实例>");
lbInstances.Enabled = false;
// Add counters now
PerformanceCounter[] pcs = pcc.GetCounters();
for (int i = 0; i < pcs.Length; i++)
{
lbCounters.Items.Add(pcs[i].CounterName);
}
// Select first item by default
lbCounters.SelectedIndex = 0;
lbCounters.Enabled = true;
}
}
}
private void lbInstances_SelectedIndexChanged(object sender, System.EventArgs e)
{
PerformanceCounter[] pcs = null;
PerformanceCounterCategory pcc = new PerformanceCounterCategory(lbCategories.SelectedItem.ToString());
lbCounters.Items.Clear();
try
{
// Enumerate counters
pcs = pcc.GetCounters(lbInstances.SelectedItem.ToString());
}
catch (Exception ex)
{
// Instance may not present at the moment
MessageBox.Show("无法枚举指定实例的计数器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
lbCounters.Items.Add("<错误>");
lbCounters.Enabled = false;
sCatName = "";
sInstName = "";
sCntName = "";
lblSelectedCounter.Text = Form1.BuildCounterString(sCatName, sInstName, sCntName);
return;
}
// Add counters
for (int i = 0; i < pcs.Length; i++)
{
lbCounters.Items.Add(pcs[i].CounterName);
}
// Select first item by default
lbCounters.SelectedIndex = 0;
lbCounters.Enabled = true;
}
private void lbCounters_SelectedIndexChanged(object sender, System.EventArgs e)
{
sCatName = lbCategories.SelectedItem.ToString();
sCntName = lbCounters.SelectedItem.ToString();
if (lbInstances.Items.Count == 1 && lbInstances.Items[0].ToString().Equals("<默认实例>"))
{
sInstName = "";
}
else
{
sInstName = lbInstances.SelectedItem.ToString();
}
lblSelectedCounter.Text = Form1.BuildCounterString(sCatName, sInstName, sCntName);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -