473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
15 4859
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Behrang Dadsetan | last post by:
Hi all, please note that once I finished writing the script someone explained me that there is a command pwait under Solaris... anyway it was fun to write and it did not take long (while I am...
3
by: Bruce Pullen | last post by:
db27.2 fp7 on AIX5.2 I just bounced the database (as a specific db2dari process had reached user memory limit), and some (orpahned) db2dari processes are still there. db2fenc1 42682 1...
1
by: Indepth | last post by:
Overview: We're creating a ASP.NET app in C# that calls some C# programs we've created. Our DB server is on a w2k3 machine and using our w2k machines as our web server. Without setting an Identity...
4
by: AN | last post by:
Greetings, We make an ASP.NET web application and we host it for our customers. We have provisioned hardware and hope to be able to service around 200 customers on this hardware. The web...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
1
by: Joshua Ruppert | last post by:
Does anyone know what it means if I see PHP processes in the process tree at the base level, instead of under the IIS worker process? I do nothing but web serving off of the box. These...
0
by: Innes MacKenzie | last post by:
I have an application (a .NET 2.0 windows service) which launches numerous short-lived processes (also a .NET 2.0 application). Occasionally, one or two of these processes will not run properly but...
5
by: Sune | last post by:
Hi all, I want to make data stored in-memory (not disk) available to several processes. My concern is that poorly written C applications with dangling pointers may(will) damage the data in this...
1
by: Feresca | last post by:
Hi everyone, I've been trying to find a way to distinguish between processes(or applications) that are just running and processes that are actually doing something. For example how can you...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.