一个进程cpu使用 [英] a processes cpu usage

查看:51
本文介绍了一个进程cpu使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


为什么这段代码不正常?


private int GetCpuUsage(Process proc)

{

DateTime time1,time2;

TimeSpan timediff;

double cpu1,cpu2,cpudiff;


time1 = DateTime.Now;

cpu1 = proc.TotalProcessorTime.TotalMilliseconds;


Thread.Sleep(100);

proc.Refresh();


time2 = DateTime.Now;

cpu2 = proc.TotalProcessorTime.TotalMilliseconds;


cpudiff = cpu2 - cpu1;

timediff = time2 - time1;


返回Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);

}


我的错误值...也许空闲进程的平均CPU使用率为

2950 %????

解决方案

你好Dirk


你得到什么样的错误价值?你的代码会产生一致的返回0

结果,这是好的。

因为我的机器上的结果值小于1%,所以转换为

Int32它给出了0.


HTH

Alex


" Dirk Reske" < _F ******* @ gmx.net>在消息中写道

新闻:OS ************** @ TK2MSFTNGP12.phx.gbl ...

你好,
为什么这段代码不能正常工作?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff ;
双cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep( 100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

返回Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
我得到错误的值...也许空闲进程的平均CPU使用率为2950%????



但是我得到一些超过100%的过程,这是不可能的......

也许是Idl电子过程总是接近2600%


" AlexS" < SA *********** @ SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag

新闻:#h ************** @ tk2msftngp13.phx.gbl ...

嗨Dirk 0结果,这是好的。
因为我的机器上的结果值小于1%,所以当转换
到Int32时它给出了0.

HTH
Alex

Dirk Reske < _F ******* @ gmx.net>在消息中写道
新闻:OS ************** @ TK2MSFTNGP12.phx.gbl ...

你好,

为什么这段代码不正常?

private int GetCpuUsage(Process proc)
{DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

返回Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}

我得错了值.. 。也许空闲进程的平均CPU使用率为
为2950%????




< blockquote> Dirk,


判断你用这个代码的一些修改你发布。我会

建议看看有什么不同。


当我做一些测试时,我从未超过2%。通常(10分中的8分) - 0.


HTH

Alex


" Dirk Reske" < _F ******* @ gmx.net>在消息中写道

news:uW ************** @ tk2msftngp13.phx.gbl ...

但是我得到了一些处理超过100%,这是不可能的......
也许空闲过程总是接近2600%

AlexS < SA *********** @ SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
新闻:#h ************** @ tk2msftngp13.phx.gbl ...

嗨Dirk
返回

0

结果,这是好的。
因为我机器上的结果值小于1%,所以当转换


Int32它给出了0.

HTH
Alex

Dirk Reske ; < _F ******* @ gmx.net>在消息中写道
新闻:OS ************** @ TK2MSFTNGP12.phx.gbl ...

你好,

为什么这段代码不正常?

private int GetCpuUsage(Process proc)
{DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

返回Convert.ToInt32(100 / timediff.TotalMilliseconds *
cpudiff);我得错了值......也许空闲进程的平均cpu使用率


为2950%????





Hello,

why doesn''t this code work correctly?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}

I get wrong values...perhaps the Idle process has an average cpu usage of
2950% ????

解决方案

Hi Dirk

what kind of wrong value you get? Your code produces consistently returns 0
as result, which is Ok.
Because resulting value on my machine is less than 1%, so when converted to
Int32 it gives 0.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl...

Hello,

why doesn''t this code work correctly?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}

I get wrong values...perhaps the Idle process has an average cpu usage of
2950% ????



But I get at some processes more than 100%, which is not possible...
Perhaps the Idle process is always nearly 2600%

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#h**************@tk2msftngp13.phx.gbl...

Hi Dirk

what kind of wrong value you get? Your code produces consistently returns 0 as result, which is Ok.
Because resulting value on my machine is less than 1%, so when converted to Int32 it gives 0.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl...

Hello,

why doesn''t this code work correctly?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}

I get wrong values...perhaps the Idle process has an average cpu usage of 2950% ????




Dirk,

judging by this you use some modification of the code you posted. I would
suggest to look what is different.

When I did some tests I never had more than 2%. Usually (8 out of 10) - 0.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...

But I get at some processes more than 100%, which is not possible...
Perhaps the Idle process is always nearly 2600%

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#h**************@tk2msftngp13.phx.gbl...

Hi Dirk

what kind of wrong value you get? Your code produces consistently returns

0

as result, which is Ok.
Because resulting value on my machine is less than 1%, so when converted


to

Int32 it gives 0.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl...

Hello,

why doesn''t this code work correctly?

private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;

time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;

Thread.Sleep(100);
proc.Refresh();

time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;

cpudiff = cpu2 - cpu1;
timediff = time2 - time1;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff); }

I get wrong values...perhaps the Idle process has an average cpu usage


of 2950% ????





这篇关于一个进程cpu使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆