using
System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Threading;
namespace WNSS
{
class CpuLoadInfo
{
//性能计数器字符串
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total";
/// <summary>
/// 获取CPU利用率
/// </summary>
/// <returns></returns>
[STAThread]
public static string getCpuInfo()
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName,InstanceName);
WNSSDataSetTableAdapters.CPUTableAdapter cpuAdapter = new WNSS.WNSSDataSetTableAdapters.CPUTableAdapter();
//string str = "";
while (true)//保证持续运转
{
Thread.Sleep(1000);
float cpuLoad = pc.NextValue();
cpuAdapter.Insert(Client.testClientIP,
DateTime.Now,
cpuLoad);
//str=str+ "CPU load = " + cpuLoad + " %. ";
}
//throw new System.NotImplementedException();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;
using System.Threading;
namespace WNSS
{
class CpuLoadInfo
{
//性能计数器字符串
private const string CategoryName = "Processor";
private const string CounterName = "% Processor Time";
private const string InstanceName = "_Total";
/// <summary>
/// 获取CPU利用率
/// </summary>
/// <returns></returns>
[STAThread]
public static string getCpuInfo()
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName,InstanceName);
WNSSDataSetTableAdapters.CPUTableAdapter cpuAdapter = new WNSS.WNSSDataSetTableAdapters.CPUTableAdapter();
//string str = "";
while (true)//保证持续运转
{
Thread.Sleep(1000);
float cpuLoad = pc.NextValue();
cpuAdapter.Insert(Client.testClientIP,
DateTime.Now,
cpuLoad);
//str=str+ "CPU load = " + cpuLoad + " %. ";
}
//throw new System.NotImplementedException();
}
}
}
本来在网上还搜到的一个使用WMI的方法,但使用后总是显示0,这个还不错