获取设备的cpu、内存、网络和硬盘使用情况,存放在单独的SystemInfo.cs文件中using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;namespace WindowsFormsApplication1
{
class SystemInfo
{
private PerformanceCounter pCPU;
private PerformanceCounter pMemory;
private PerformanceCounter pDisk;
private PerformanceCounter pNetWorkReceive;
private PerformanceCounter pNetWorkSend; private float _cpuUsed;
private float _memoryUsed;
private float _diskUsed;
private float _netReceiveSpeed;
private float _netSendSpeed; private SystemInfo()
{
} private static SystemInfo singleInstance = new SystemInfo(); public static SystemInfo GetInstance()
{
singleInstance.WatchCPU();
singleInstance.WatchMemory();
singleInstance.WatchDisk();
singleInstance.WatchNetWork(); return singleInstance;
} public float GetCPU
{
get { return _cpuUsed; }
} public float GetMem
{
get { return _memoryUsed; }
} public float GetDisk
{
get { return _diskUsed; }
} public float GetSendSpeed
{
get { return _netSendSpeed; }
} public float GetReceiveSpeed
{
get { return _netReceiveSpeed; }
} private void WatchCPU()
{
pCPU = new PerformanceCounter();
pCPU.CategoryName = "Processor";
pCPU.CounterName = "% Processor Time";
pCPU.InstanceName = "_Total";
_cpuUsed = pCPU.NextValue();
} private void WatchMemory()
{
pMemory = new PerformanceCounter();
pMemory.CategoryName = "Memory";
pMemory.CounterName = "Available MBytes";
_memoryUsed = pMemory.NextValue();
} private void WatchDisk()
{
pDisk = new PerformanceCounter();
pDisk.CategoryName = "LogicalDisk";
pDisk.CounterName = "% Free Space";
pDisk.InstanceName = "_Total";
_diskUsed = pDisk.NextValue();
} private void WatchNetWork()
{
_netReceiveSpeed = 0;
_netSendSpeed = 0; pNetWorkReceive = new PerformanceCounter();
pNetWorkReceive.CategoryName = "Network Interface";
pNetWorkReceive.CounterName = "Bytes Received/sec"; PerformanceCounterCategory oneCategory = new PerformanceCounterCategory(pNetWorkReceive.CategoryName);
string[] allInstances = oneCategory.GetInstanceNames(); pNetWorkSend = new PerformanceCounter();
pNetWorkSend.CategoryName = "Network Interface";
pNetWorkSend.CounterName = "Bytes Sent/sec"; for (int i = 0; i < allInstances.Length; i++)
{
pNetWorkReceive.InstanceName = allInstances[i];
_netReceiveSpeed += pNetWorkReceive.NextValue(); pNetWorkSend.InstanceName = allInstances[i];
_netSendSpeed += pNetWorkSend.NextValue();
}
}
现在调用这个类,将数据展示到界面上
界面代码如下:delegate void TransferValue(); private void NewForm_Load(object sender, EventArgs e)
{
Thread td = new Thread(new ThreadStart(GetInfo));
td.Start();
} private void GetInfo()
{
while (true)
{
cpuInfo = SystemInfo.GetInstance().GetCPU;
diskInfo = SystemInfo.GetInstance().GetDisk;
memoryInfo = SystemInfo.GetInstance().GetMem;
receieInfo = SystemInfo.GetInstance().GetReceiveSpeed;
sendInfo = SystemInfo.GetInstance().GetSendSpeed; labelCPU.Invoke(new TransferValue(UpdateUI));
Thread.Sleep(1000);
}
} private void UpdateUI()
{
labelCPU.Text = cpuInfo.ToString() + "%";
labelDisk.Text = diskInfo.ToString();
}现在的问题是,为什么系统数据不刷新?是不是使用单例模式的时候getinstance方法不会间隔1秒后执行?像这种情况该如何处理?谢谢了。
t.Change(1000, 1000);
private void TimerProc(object state)
{}
延迟执行
timer刷新数据
改成timer还是不行,和使用线程一样,只有memory数据可以更新,其它都不可以,不知道为什么,下面是窗体代码,麻烦帮我看看是什么问题,最好能试一下,感激不尽!!public partial class NewForm : Form
{
public NewForm()
{
InitializeComponent();
} private float cpuInfo;
private float diskInfo;
private float memoryInfo;
private float receieInfo;
private float sendInfo;
delegate void TransferValue(); private void NewForm_Load(object sender, EventArgs e)
{
//Thread td = new Thread(new ThreadStart(GetInfo));
//td.Start();
System.Threading.Timer t = new System.Threading.Timer(new TimerCallback(GetInfo));
t.Change(1000,1000);
} private void GetInfo(object obj)
{
//while (true)
//{
SystemInfo.GetInstance.DoWork();
cpuInfo = SystemInfo.GetInstance.GetCPU;
diskInfo = SystemInfo.GetInstance.GetDisk;
memoryInfo = SystemInfo.GetInstance.GetMem;
receieInfo = SystemInfo.GetInstance.GetReceiveSpeed;
sendInfo = SystemInfo.GetInstance.GetSendSpeed; labelDisk.Invoke(new TransferValue(UpdateUI));
// Thread.Sleep(1000);
//}
} private void UpdateUI()
{
labelCPU.Text = cpuInfo.ToString() + "%";
labelMem.Text = memoryInfo.ToString();
labelNetReceive.Text = receieInfo.ToString();
labelNetSend.Text = sendInfo.ToString();
labelDisk.Text = diskInfo.ToString();
}
}
{
if (pCPU == null)
{
pCPU = new PerformanceCounter();
pCPU.CategoryName = "Processor";
pCPU.CounterName = "% Processor Time";
pCPU.InstanceName = "_Total";
} _cpuUsed = pCPU.NextValue();
}保证所有的PerformanceCounter只被实例化1次就可以了,不要每次调用都重新产生新的实例。
用定时器通过NextValue()来获取更新。