467,169 Members | 959 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

a processes cpu usage

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% ????
Nov 16 '05 #1
  • viewed: 4435
Share:
15 Replies
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% ????

Nov 16 '05 #2
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% ????


Nov 16 '05 #3
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% ????



Nov 16 '05 #4
No, I really use this code:

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OI**************@tk2msftngp13.phx.gbl...
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% ????
>
>



Nov 16 '05 #5
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);
}
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OI**************@tk2msftngp13.phx.gbl...
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% ????
>
>



Nov 16 '05 #6
Dirk,

then take a good look which process you pass in. I tested with current
process. Code is fine with it.
However if you pass reference of process which was obtained long before the
call and was not refreshed since - you might get huge value in cpudiff. Take
a good look at process.Refresh method description.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:Ol**************@TK2MSFTNGP09.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;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OI**************@tk2msftngp13.phx.gbl...
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% ????
> >
> >
>
>



Nov 16 '05 #7
I pass each process...
foreach (Process proc in Process.GetProcesses())
......

when I put a proc.Refresh() at the first line of the GetCpuUsage funktion
all works fine...
some little mistakes..

System: 10%
Idle: 100%

-> this are 110% this can't be??

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#u**************@TK2MSFTNGP12.phx.gbl...
Dirk,

then take a good look which process you pass in. I tested with current
process. Code is fine with it.
However if you pass reference of process which was obtained long before the call and was not refreshed since - you might get huge value in cpudiff. Take a good look at process.Refresh method description.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:Ol**************@TK2MSFTNGP09.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;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OI**************@tk2msftngp13.phx.gbl...
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% ????
> > >
> > >
> >
> >
>
>



Nov 16 '05 #8
Dirk,

if you want to measure activity of process - you must measure it using as a
start time when process was started, not some arbitrary moment as in your
sample. When you go through all processes on machine and measure activity
against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since
the process started + some delta which process used during sleep.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u6*************@tk2msftngp13.phx.gbl...
I pass each process...
foreach (Process proc in Process.GetProcesses())
.....

when I put a proc.Refresh() at the first line of the GetCpuUsage funktion
all works fine...
some little mistakes..

System: 10%
Idle: 100%

-> this are 110% this can't be??

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#u**************@TK2MSFTNGP12.phx.gbl...
Dirk,

then take a good look which process you pass in. I tested with current
process. Code is fine with it.
However if you pass reference of process which was obtained long before

the
call and was not refreshed since - you might get huge value in cpudiff.

Take
a good look at process.Refresh method description.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:Ol**************@TK2MSFTNGP09.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;

return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff); }
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OI**************@tk2msftngp13.phx.gbl...
> 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% ????
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #9
acn you modify my code so?

thx

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Oy**************@TK2MSFTNGP11.phx.gbl...
Dirk,

if you want to measure activity of process - you must measure it using as a start time when process was started, not some arbitrary moment as in your
sample. When you go through all processes on machine and measure activity
against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since
the process started + some delta which process used during sleep.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u6*************@tk2msftngp13.phx.gbl...
I pass each process...
foreach (Process proc in Process.GetProcesses())
.....

when I put a proc.Refresh() at the first line of the GetCpuUsage funktion
all works fine...
some little mistakes..

System: 10%
Idle: 100%

-> this are 110% this can't be??

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#u**************@TK2MSFTNGP12.phx.gbl...
Dirk,

then take a good look which process you pass in. I tested with current
process. Code is fine with it.
However if you pass reference of process which was obtained long before
the
call and was not refreshed since - you might get huge value in
cpudiff. Take
a good look at process.Refresh method description.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:Ol**************@TK2MSFTNGP09.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;
>
> return Convert.ToInt32(100 / timediff.TotalMilliseconds *

cpudiff); > }
> "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im
Newsbeitrag > news:OI**************@tk2msftngp13.phx.gbl...
> > 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% ????
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #10
Dirk,

take a look at Process.StartTime property. Then I am pretty sure you can
modify your code yourself as you like.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
acn you modify my code so?

thx

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Oy**************@TK2MSFTNGP11.phx.gbl...
Dirk,

if you want to measure activity of process - you must measure it using as
a
start time when process was started, not some arbitrary moment as in your sample. When you go through all processes on machine and measure activity against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since the process started + some delta which process used during sleep.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u6*************@tk2msftngp13.phx.gbl...
I pass each process...
foreach (Process proc in Process.GetProcesses())
.....

when I put a proc.Refresh() at the first line of the GetCpuUsage funktion all works fine...
some little mistakes..

System: 10%
Idle: 100%

-> this are 110% this can't be??

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#u**************@TK2MSFTNGP12.phx.gbl...
> Dirk,
>
> then take a good look which process you pass in. I tested with current > process. Code is fine with it.
> However if you pass reference of process which was obtained long before the
> call and was not refreshed since - you might get huge value in cpudiff. Take
> a good look at process.Refresh method description.
>
> HTH
> Alex
>
> "Dirk Reske" <_F*******@gmx.net> wrote in message
> news:Ol**************@TK2MSFTNGP09.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;
> >
> > return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
> > }
> > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag > > news:OI**************@tk2msftngp13.phx.gbl...
> > > 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% ????
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #11
but than I get an average value...or not?
I want the actual value...
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Og**************@TK2MSFTNGP12.phx.gbl...
Dirk,

take a look at Process.StartTime property. Then I am pretty sure you can
modify your code yourself as you like.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
acn you modify my code so?

thx

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Oy**************@TK2MSFTNGP11.phx.gbl...
Dirk,

if you want to measure activity of process - you must measure it using as
a
start time when process was started, not some arbitrary moment as in

your sample. When you go through all processes on machine and measure activity against 100ms interval of course you get senseless figures.
Check once again Refresh. In your foreach you get total milliseconds since the process started + some delta which process used during sleep.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:u6*************@tk2msftngp13.phx.gbl...
> I pass each process...
> foreach (Process proc in Process.GetProcesses())
> .....
>
> when I put a proc.Refresh() at the first line of the GetCpuUsage

funktion
> all works fine...
> some little mistakes..
>
> System: 10%
> Idle: 100%
>
> -> this are 110% this can't be??
>
> "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag > news:#u**************@TK2MSFTNGP12.phx.gbl...
> > Dirk,
> >
> > then take a good look which process you pass in. I tested with current > > process. Code is fine with it.
> > However if you pass reference of process which was obtained long

before
> the
> > call and was not refreshed since - you might get huge value in

cpudiff.
> Take
> > a good look at process.Refresh method description.
> >
> > HTH
> > Alex
> >
> > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > news:Ol**************@TK2MSFTNGP09.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;
> > >
> > > return Convert.ToInt32(100 / timediff.TotalMilliseconds *
cpudiff);
> > > }
> > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im

Newsbeitrag
> > > news:OI**************@tk2msftngp13.phx.gbl...
> > > > 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% ????
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #12
Right, then you get average value.

If you want snapshot - I would suggest a bit different approach:
- get list of processes and times
- delay for 1 sec - better do it using thread timer than thread sleep
- get another list of processes and times
- compare both list and calculate percentage for each existing process in
second array using either related process Total from first array or process
start time whatever is latest and saved timestamps.
In reality process is even more complex if you want to have really precise
picture. However it is not very clear what for so much effort?

Maybe it would be easier to use Task Manager?
Actual value is always some kind of approximation
HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl...
but than I get an average value...or not?
I want the actual value...
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Og**************@TK2MSFTNGP12.phx.gbl...
Dirk,

take a look at Process.StartTime property. Then I am pretty sure you can
modify your code yourself as you like.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
acn you modify my code so?

thx

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Oy**************@TK2MSFTNGP11.phx.gbl...
> Dirk,
>
> if you want to measure activity of process - you must measure it
using
as
a
> start time when process was started, not some arbitrary moment as in

your
> sample. When you go through all processes on machine and measure

activity
> against 100ms interval of course you get senseless figures.
> Check once again Refresh. In your foreach you get total milliseconds

since
> the process started + some delta which process used during sleep.
>
> HTH
> Alex
>
> "Dirk Reske" <_F*******@gmx.net> wrote in message
> news:u6*************@tk2msftngp13.phx.gbl...
> > I pass each process...
> > foreach (Process proc in Process.GetProcesses())
> > .....
> >
> > when I put a proc.Refresh() at the first line of the GetCpuUsage
funktion
> > all works fine...
> > some little mistakes..
> >
> > System: 10%
> > Idle: 100%
> >
> > -> this are 110% this can't be??
> >
> > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag > > news:#u**************@TK2MSFTNGP12.phx.gbl...
> > > Dirk,
> > >
> > > then take a good look which process you pass in. I tested with

current
> > > process. Code is fine with it.
> > > However if you pass reference of process which was obtained long
before
> > the
> > > call and was not refreshed since - you might get huge value in
cpudiff.
> > Take
> > > a good look at process.Refresh method description.
> > >
> > > HTH
> > > Alex
> > >
> > > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > > news:Ol**************@TK2MSFTNGP09.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;
> > > >
> > > > return Convert.ToInt32(100 / timediff.TotalMilliseconds *
> cpudiff);
> > > > }
> > > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im
Newsbeitrag
> > > > news:OI**************@tk2msftngp13.phx.gbl...
> > > > > 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% ????
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #13
Yes, but I need this values...
If there were any way to get the values from the taskmanager, I would use
it.
And I need the filename of the main module...
there are also performance counters that represent the cpu usage of each
process...but there I cant get the filenames,
because if the same process is running more than one time, the performance
counter are named svchost#1 #2 and so on....
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OZ*************@TK2MSFTNGP11.phx.gbl...
Right, then you get average value.

If you want snapshot - I would suggest a bit different approach:
- get list of processes and times
- delay for 1 sec - better do it using thread timer than thread sleep
- get another list of processes and times
- compare both list and calculate percentage for each existing process in
second array using either related process Total from first array or process start time whatever is latest and saved timestamps.
In reality process is even more complex if you want to have really precise
picture. However it is not very clear what for so much effort?

Maybe it would be easier to use Task Manager?
Actual value is always some kind of approximation
HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl...
but than I get an average value...or not?
I want the actual value...
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Og**************@TK2MSFTNGP12.phx.gbl...
Dirk,

take a look at Process.StartTime property. Then I am pretty sure you can modify your code yourself as you like.

HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
> acn you modify my code so?
>
> thx
>
> "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag > news:Oy**************@TK2MSFTNGP11.phx.gbl...
> > Dirk,
> >
> > if you want to measure activity of process - you must measure it using as
> a
> > start time when process was started, not some arbitrary moment as in your
> > sample. When you go through all processes on machine and measure
activity
> > against 100ms interval of course you get senseless figures.
> > Check once again Refresh. In your foreach you get total milliseconds since
> > the process started + some delta which process used during sleep.
> >
> > HTH
> > Alex
> >
> > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > news:u6*************@tk2msftngp13.phx.gbl...
> > > I pass each process...
> > > foreach (Process proc in Process.GetProcesses())
> > > .....
> > >
> > > when I put a proc.Refresh() at the first line of the GetCpuUsage
> funktion
> > > all works fine...
> > > some little mistakes..
> > >
> > > System: 10%
> > > Idle: 100%
> > >
> > > -> this are 110% this can't be??
> > >
> > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im

Newsbeitrag
> > > news:#u**************@TK2MSFTNGP12.phx.gbl...
> > > > Dirk,
> > > >
> > > > then take a good look which process you pass in. I tested with
current
> > > > process. Code is fine with it.
> > > > However if you pass reference of process which was obtained long > before
> > > the
> > > > call and was not refreshed since - you might get huge value in
> cpudiff.
> > > Take
> > > > a good look at process.Refresh method description.
> > > >
> > > > HTH
> > > > Alex
> > > >
> > > > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > > > news:Ol**************@TK2MSFTNGP09.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;
> > > > >
> > > > > return Convert.ToInt32(100 / timediff.TotalMilliseconds * > > cpudiff);
> > > > > }
> > > > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im
> Newsbeitrag
> > > > > news:OI**************@tk2msftngp13.phx.gbl...
> > > > > > 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% ????
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #14
Dirk,

please take a look at your code once more,
Looks like you call your routine from foreach loop - which means that you
get statistics for different processes during different intervals. I think
it is the issue.

Your routine as posted is Ok for measuring single process %. However you did
not post code which uses it.

I hope now it makes things a bit more clear for you. And sorry for
StartTime - it is not much relevant for you - I hoped it would help you to
find out where you make mistake in reasoning together with Refresh
description.

So you have to loop through all processes getting most recent data, then do
some delay, then loop once again (new list processes or refreshes for all of
them). Only then you will get balanced figures. And then some processes may
disappear during measuring interval and other start. So it will be not that
simple.

Cheers!
Alex
PS: "Wise are learning from mistakes of others, not very wise - from their
own"

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:Or**************@TK2MSFTNGP12.phx.gbl...
Yes, but I need this values...
If there were any way to get the values from the taskmanager, I would use
it.
And I need the filename of the main module...
there are also performance counters that represent the cpu usage of each
process...but there I cant get the filenames,
because if the same process is running more than one time, the performance
counter are named svchost#1 #2 and so on....
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:OZ*************@TK2MSFTNGP11.phx.gbl...
Right, then you get average value.

If you want snapshot - I would suggest a bit different approach:
- get list of processes and times
- delay for 1 sec - better do it using thread timer than thread sleep
- get another list of processes and times
- compare both list and calculate percentage for each existing process in
second array using either related process Total from first array or process
start time whatever is latest and saved timestamps.
In reality process is even more complex if you want to have really precise picture. However it is not very clear what for so much effort?

Maybe it would be easier to use Task Manager?
Actual value is always some kind of approximation
HTH
Alex

"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OF**************@TK2MSFTNGP09.phx.gbl...
but than I get an average value...or not?
I want the actual value...
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:Og**************@TK2MSFTNGP12.phx.gbl...
> Dirk,
>
> take a look at Process.StartTime property. Then I am pretty sure you can > modify your code yourself as you like.
>
> HTH
> Alex
>
> "Dirk Reske" <_F*******@gmx.net> wrote in message
> news:ee**************@tk2msftngp13.phx.gbl...
> > acn you modify my code so?
> >
> > thx
> >
> > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag > > news:Oy**************@TK2MSFTNGP11.phx.gbl...
> > > Dirk,
> > >
> > > if you want to measure activity of process - you must measure it

using
> as
> > a
> > > start time when process was started, not some arbitrary moment as in
> your
> > > sample. When you go through all processes on machine and measure
> activity
> > > against 100ms interval of course you get senseless figures.
> > > Check once again Refresh. In your foreach you get total milliseconds > since
> > > the process started + some delta which process used during
sleep. > > >
> > > HTH
> > > Alex
> > >
> > > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > > news:u6*************@tk2msftngp13.phx.gbl...
> > > > I pass each process...
> > > > foreach (Process proc in Process.GetProcesses())
> > > > .....
> > > >
> > > > when I put a proc.Refresh() at the first line of the GetCpuUsage > > funktion
> > > > all works fine...
> > > > some little mistakes..
> > > >
> > > > System: 10%
> > > > Idle: 100%
> > > >
> > > > -> this are 110% this can't be??
> > > >
> > > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im
Newsbeitrag
> > > > news:#u**************@TK2MSFTNGP12.phx.gbl...
> > > > > Dirk,
> > > > >
> > > > > then take a good look which process you pass in. I tested with > current
> > > > > process. Code is fine with it.
> > > > > However if you pass reference of process which was obtained
long > > before
> > > > the
> > > > > call and was not refreshed since - you might get huge value in > > cpudiff.
> > > > Take
> > > > > a good look at process.Refresh method description.
> > > > >
> > > > > HTH
> > > > > Alex
> > > > >
> > > > > "Dirk Reske" <_F*******@gmx.net> wrote in message
> > > > > news:Ol**************@TK2MSFTNGP09.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;
> > > > > >
> > > > > > return Convert.ToInt32(100 / timediff.TotalMilliseconds *
> > > cpudiff);
> > > > > > }
> > > > > > "AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im
> > Newsbeitrag
> > > > > > news:OI**************@tk2msftngp13.phx.gbl...
> > > > > > > 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% ????
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #15
Perphaps you have already solved your problem by now. Anyway, I came
across your posting when I was lookin for the information on how to
calculate process CPU usage. My code is essential the same as yours
except that mine multiply the system elapsed time (your cpudiff) by the
number of processors. Without the processors multipler, I was getting
similiar incorrect values which exceeded 100%. Then I realized that
the OS (or the BIOS) reported 2 processors for my hyperthreading CPU.

Nov 16 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Behrang Dadsetan | last post: by
3 posts views Thread by Bruce Pullen | last post: by
35 posts views Thread by Carl J. Van Arsdall | last post: by
1 post views Thread by Joshua Ruppert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.