1个回答
展开全部
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
private void EnumProcesses()
{
// Timespans for individual process information
TimeSpan tpt;
TimeSpan tppt;
TimeSpan tupt;
// Timespans for machine
TimeSpan mtpt = new TimeSpan();
TimeSpan mtppt = new TimeSpan();
TimeSpan mtupt = new TimeSpan();
ListViewItem lSingleItem;
// Enumerate all processes
try
{
Process[] Processes;
if (!(mcolProcesses == null))
{
mcolProcesses = new ArrayList();
}
if (this.lvProcesses.Items.Count > 0)
{
this.lvProcesses.Items.Clear();
this.lvProcessDetail.Items.Clear();
this.lvThreads.Items.Clear();
}
Processes = Process.GetProcesses();
foreach(Process p in Processes)
{
mcolProcesses.Add(p);
// Get processor time and store
tppt = p.PrivilegedProcessorTime;
tupt = p.UserProcessorTime;
tpt = p.TotalProcessorTime;
// Add the current process times to total times.
mtpt = mtpt.Add(tpt);
mtppt = mtppt.Add(tppt);
mtupt = mtupt.Add(tupt);
// % User Processor Time
decimal dblPUPT = Decimal.Divide(tupt.Ticks, tpt.Ticks);
string strPUPT = dblPUPT.ToString("#0%");
// % Privileged Processor Time
decimal dblPPPT = Decimal.Divide(tppt.Ticks, tpt.Ticks);
string strPPPT = dblPPPT.ToString("#0%");
string strTPT;
strTPT = (tpt.Days.ToString("00") + "." + tpt.Hours.ToString("00") + ":" + tpt.Minutes.ToString("00") + ":" + tpt.Seconds.ToString("00"));
lSingleItem = this.lvProcesses.Items.Add(p.ProcessName + " (0x" + (p.Id.ToString("x")).ToLower() + ")");
lSingleItem.SubItems.Add(p.Id.ToString());
lSingleItem.SubItems.Add(strTPT);
lSingleItem.SubItems.Add(strPPPT);
lSingleItem.SubItems.Add(strPUPT);
}
// % Total User Processor Time
decimal mdecPUPT = Decimal.Divide(mtupt.Ticks , mtpt.Ticks);
string mstrPUPT = mdecPUPT.ToString("#0%");
// % Total Privileged Processor Time
decimal mdecPPPT = decimal.Divide(mtppt.Ticks,mtpt.Ticks);
string mstrPPPT = mdecPPPT.ToString("#0%");
string mstrTPT;
mstrTPT = (mtpt.Days.ToString("00") + "." + mtpt.Hours.ToString("00") + ":" + mtpt.Minutes.ToString("00") + ":" + mtpt.Seconds.ToString("00"));
// Add entry for all processes
lSingleItem = this.lvProcesses.Items.Add(PROCESS_NAME_TOTAL);
lSingleItem.SubItems.Add(PID_NA);
lSingleItem.SubItems.Add(mstrTPT);
lSingleItem.SubItems.Add(mstrPPPT);
lSingleItem.SubItems.Add(mstrPUPT);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
c# 检测cpu使用率
创建一个控制台应用程序,代码如下
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引用2命名空间
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process[] p = Process.GetProcessesByName("devenv");//获取指定进程信息
// Process[] p = Process.GetProcesses();//获取所有进程信息
string cpu = string.Empty;
string info = string.Empty;
PerformanceCounter pp = new PerformanceCounter();//性能计数器
pp.CategoryName = "Process";//指定获取计算机进程信息如果传Processor参数代表查询计算机CPU
pp.CounterName = "% Processor Time";//占有率
//如果pp.CategoryName="Processor",那么你这里赋值这个参数 pp.InstanceName = "_Total"代表查询本计算机的总CPU。
pp.InstanceName = "devenv";//指定进程
pp.MachineName = ".";
if (p.Length > 0)
{
foreach (Process pr in p)
{
while (true)//1秒钟读取一次CPU占有率。
{
info = pr.ProcessName + "内存:" +
(Convert.ToInt64(pr.WorkingSet64.ToString()) / 1024).ToString();//得到进程内存
Console.WriteLine(info + " CPU使用情况:" + Math.Round(pp.NextValue(), 2).ToString() + "%");
Thread.Sleep(1000);
}
}
}
}
}
}
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
private void EnumProcesses()
{
// Timespans for individual process information
TimeSpan tpt;
TimeSpan tppt;
TimeSpan tupt;
// Timespans for machine
TimeSpan mtpt = new TimeSpan();
TimeSpan mtppt = new TimeSpan();
TimeSpan mtupt = new TimeSpan();
ListViewItem lSingleItem;
// Enumerate all processes
try
{
Process[] Processes;
if (!(mcolProcesses == null))
{
mcolProcesses = new ArrayList();
}
if (this.lvProcesses.Items.Count > 0)
{
this.lvProcesses.Items.Clear();
this.lvProcessDetail.Items.Clear();
this.lvThreads.Items.Clear();
}
Processes = Process.GetProcesses();
foreach(Process p in Processes)
{
mcolProcesses.Add(p);
// Get processor time and store
tppt = p.PrivilegedProcessorTime;
tupt = p.UserProcessorTime;
tpt = p.TotalProcessorTime;
// Add the current process times to total times.
mtpt = mtpt.Add(tpt);
mtppt = mtppt.Add(tppt);
mtupt = mtupt.Add(tupt);
// % User Processor Time
decimal dblPUPT = Decimal.Divide(tupt.Ticks, tpt.Ticks);
string strPUPT = dblPUPT.ToString("#0%");
// % Privileged Processor Time
decimal dblPPPT = Decimal.Divide(tppt.Ticks, tpt.Ticks);
string strPPPT = dblPPPT.ToString("#0%");
string strTPT;
strTPT = (tpt.Days.ToString("00") + "." + tpt.Hours.ToString("00") + ":" + tpt.Minutes.ToString("00") + ":" + tpt.Seconds.ToString("00"));
lSingleItem = this.lvProcesses.Items.Add(p.ProcessName + " (0x" + (p.Id.ToString("x")).ToLower() + ")");
lSingleItem.SubItems.Add(p.Id.ToString());
lSingleItem.SubItems.Add(strTPT);
lSingleItem.SubItems.Add(strPPPT);
lSingleItem.SubItems.Add(strPUPT);
}
// % Total User Processor Time
decimal mdecPUPT = Decimal.Divide(mtupt.Ticks , mtpt.Ticks);
string mstrPUPT = mdecPUPT.ToString("#0%");
// % Total Privileged Processor Time
decimal mdecPPPT = decimal.Divide(mtppt.Ticks,mtpt.Ticks);
string mstrPPPT = mdecPPPT.ToString("#0%");
string mstrTPT;
mstrTPT = (mtpt.Days.ToString("00") + "." + mtpt.Hours.ToString("00") + ":" + mtpt.Minutes.ToString("00") + ":" + mtpt.Seconds.ToString("00"));
// Add entry for all processes
lSingleItem = this.lvProcesses.Items.Add(PROCESS_NAME_TOTAL);
lSingleItem.SubItems.Add(PID_NA);
lSingleItem.SubItems.Add(mstrTPT);
lSingleItem.SubItems.Add(mstrPPPT);
lSingleItem.SubItems.Add(mstrPUPT);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
c# 检测cpu使用率
创建一个控制台应用程序,代码如下
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//引用2命名空间
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process[] p = Process.GetProcessesByName("devenv");//获取指定进程信息
// Process[] p = Process.GetProcesses();//获取所有进程信息
string cpu = string.Empty;
string info = string.Empty;
PerformanceCounter pp = new PerformanceCounter();//性能计数器
pp.CategoryName = "Process";//指定获取计算机进程信息如果传Processor参数代表查询计算机CPU
pp.CounterName = "% Processor Time";//占有率
//如果pp.CategoryName="Processor",那么你这里赋值这个参数 pp.InstanceName = "_Total"代表查询本计算机的总CPU。
pp.InstanceName = "devenv";//指定进程
pp.MachineName = ".";
if (p.Length > 0)
{
foreach (Process pr in p)
{
while (true)//1秒钟读取一次CPU占有率。
{
info = pr.ProcessName + "内存:" +
(Convert.ToInt64(pr.WorkingSet64.ToString()) / 1024).ToString();//得到进程内存
Console.WriteLine(info + " CPU使用情况:" + Math.Round(pp.NextValue(), 2).ToString() + "%");
Thread.Sleep(1000);
}
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询