[WINFORM]如何获取当前计算机CPU的占用率? 如果能获取到远程计算机的更好~谢谢了~最好贴相关代码,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 NtQuerySystemInformation() //进程信息(包括CPU使用) WMI?能不能具体点?谢谢,获取本地的就可以了。谢谢NtQuerySystemInformation() ?能不能具体点?包括类名空间,使用方法?谢谢了~~~~~再次感谢 ! System.Diagnostics.PerformanceCounter performanceCounter1;performanceCounter1 = new System.Diagnostics.PerformanceCounter();performanceCounter1.CategoryName = "Processor";performanceCounter1.CounterName = "% Processor Time";performanceCounter1.InstanceName = "_Total";performanceCounter1.MachineName = "."; //本机performanceCounter1.NextValue(); //cpu占用率 vosov(ask a favor of wind...) give an answer in the correct way! Thanks! private string Cpu() { System.Diagnostics.PerformanceCounter performanceCounter1; performanceCounter1 = new System.Diagnostics.PerformanceCounter(); performanceCounter1.CategoryName = "Processor"; performanceCounter1.CounterName = "% Processor Time"; performanceCounter1.InstanceName = "_Total"; performanceCounter1.MachineName = "."; //本机 return performanceCounter1.NextValue().ToString(); //cpu占用率 }//这里我用一个时钟控件让它每秒更新一次,并输出到label2上 private void timer1_Tick(object sender, System.EventArgs e) { this.label2.Text = Cpu(); }//最后结果是总是显示为"0",是哪里出了问题呢?能不能更详细地把那些参数说明了一下?//再次感谢 performanceCounter1定义为类成员。初始化在类的构造函数里做。Cpu()函数里应该只有一句:return this.performanceCounter1.NextValue().ToString(); 非常感谢!!修改后如下:private PerformanceCounter PC = new PerformanceCounter();private void Form1_Load(object sender, System.EventArgs e){ PC.CategoryName = "Process"; PC.CounterName = "% Processor Time"; PC.InstanceName = "_Total"; PC.MachineName = "."; this.timer1.Interval = 1000; this.timer1.Enabled = true;}private void timer1_Tick(object sender, System.EventArgs e){ this.label2.Text = PC.NextValue().ToString()+"%";}但label2的值大部分时间都维持在100%,偶尔出现98%,与实际的很不相符。查过MSDN相关,PC.CounterName 也有一个Private Byte属性,即PC.CounterName = "Private Byte";得到的结果好像是一个16进制的数~~~不知道到底哪里出错了呢?谢谢~~~~~~ 不好意思。 CategoryName 应该是 "Processor"; 嗯!我过来就是想告诉你,Process少了or,哈哈,成功了!真是非常感谢你!可惜我的分太少,有点对不住~~从心底感谢各位,特别是vosov!谢谢·! 是少了or~~Processor就能正常获取,否则总是显示100% DataRow类型向DataGridViewRow转换 Dictionary可以排序吗?(不是SortedDictionary) 关于快速显示图片的问题 C# ,在datagrid里面能不能设置公式? .Net中的委托技术---困扰多时,请高手协助! 请问C#有没有evaluate函数? 在自己的環境下沒錯 ,可以發到服務器上就explianss就是空,有那位大師知道這樣的原因? 查找字符串中字符的位置 关于异步方法调用 现在好像到这里来招聘的有增多的势头,看来C#渐渐地在被有些公司采用了 System.Array的问题,很急!!!!! 帮兄弟一个忙!!!! 如何更新进度条进度?
获取本地的就可以了。谢谢
NtQuerySystemInformation() ?
能不能具体点?包括类名空间,使用方法?谢谢了~~~~~再次感谢 !
performanceCounter1 = new System.Diagnostics.PerformanceCounter();
performanceCounter1.CategoryName = "Processor";
performanceCounter1.CounterName = "% Processor Time";
performanceCounter1.InstanceName = "_Total";
performanceCounter1.MachineName = "."; //本机
performanceCounter1.NextValue(); //cpu占用率
{
System.Diagnostics.PerformanceCounter performanceCounter1;
performanceCounter1 = new System.Diagnostics.PerformanceCounter();
performanceCounter1.CategoryName = "Processor";
performanceCounter1.CounterName = "% Processor Time";
performanceCounter1.InstanceName = "_Total";
performanceCounter1.MachineName = "."; //本机
return performanceCounter1.NextValue().ToString(); //cpu占用率
}//这里我用一个时钟控件让它每秒更新一次,并输出到label2上
private void timer1_Tick(object sender, System.EventArgs e)
{
this.label2.Text = Cpu();
}//最后结果是总是显示为"0",是哪里出了问题呢?能不能更详细地把那些参数说明了一下?
//再次感谢
{
PC.CategoryName = "Process";
PC.CounterName = "% Processor Time";
PC.InstanceName = "_Total";
PC.MachineName = ".";
this.timer1.Interval = 1000;
this.timer1.Enabled = true;
}private void timer1_Tick(object sender, System.EventArgs e)
{
this.label2.Text = PC.NextValue().ToString()+"%";
}但label2的值大部分时间都维持在100%,偶尔出现98%,与实际的很不相符。
查过MSDN相关,PC.CounterName 也有一个Private Byte属性,即
PC.CounterName = "Private Byte";
得到的结果好像是一个16进制的数~~~
不知道到底哪里出错了呢?谢谢~~~~~~
哈哈,成功了!
真是非常感谢你!
可惜我的分太少,有点对不住~~
从心底感谢各位,特别是vosov!
谢谢·!
Processor
就能正常获取,否则总是显示100%