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% ???? 15 4827
Hi Dirk
what kind of wrong value you get? Your code produces consistently returns 0
as result, which is Ok.
Because resulting value on my machine is less than 1%, so when converted to
Int32 it gives 0.
HTH
Alex
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:OS**************@TK2MSFTNGP12.phx.gbl... Hello,
why doesn't this code work correctly?
private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
time1 = DateTime.Now; cpu1 = proc.TotalProcessorTime.TotalMilliseconds;
Thread.Sleep(100); proc.Refresh();
time2 = DateTime.Now; cpu2 = proc.TotalProcessorTime.TotalMilliseconds;
cpudiff = cpu2 - cpu1; timediff = time2 - time1;
return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff); }
I get wrong values...perhaps the Idle process has an average cpu usage of 2950% ????
But I get at some processes more than 100%, which is not possible...
Perhaps the Idle process is always nearly 2600%
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag
news:#h**************@tk2msftngp13.phx.gbl... Hi Dirk
what kind of wrong value you get? Your code produces consistently returns
0 as result, which is Ok. Because resulting value on my machine is less than 1%, so when converted
to Int32 it gives 0.
HTH Alex
"Dirk Reske" <_F*******@gmx.net> wrote in message news:OS**************@TK2MSFTNGP12.phx.gbl... Hello,
why doesn't this code work correctly?
private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
time1 = DateTime.Now; cpu1 = proc.TotalProcessorTime.TotalMilliseconds;
Thread.Sleep(100); proc.Refresh();
time2 = DateTime.Now; cpu2 = proc.TotalProcessorTime.TotalMilliseconds;
cpudiff = cpu2 - cpu1; timediff = time2 - time1;
return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff); }
I get wrong values...perhaps the Idle process has an average cpu usage
of 2950% ????
Dirk,
judging by this you use some modification of the code you posted. I would
suggest to look what is different.
When I did some tests I never had more than 2%. Usually (8 out of 10) - 0.
HTH
Alex
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:uW**************@tk2msftngp13.phx.gbl... But I get at some processes more than 100%, which is not possible... Perhaps the Idle process is always nearly 2600%
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> schrieb im Newsbeitrag news:#h**************@tk2msftngp13.phx.gbl... Hi Dirk
what kind of wrong value you get? Your code produces consistently
returns 0 as result, which is Ok. Because resulting value on my machine is less than 1%, so when converted to Int32 it gives 0.
HTH Alex
"Dirk Reske" <_F*******@gmx.net> wrote in message news:OS**************@TK2MSFTNGP12.phx.gbl... Hello,
why doesn't this code work correctly?
private int GetCpuUsage(Process proc) { DateTime time1,time2; TimeSpan timediff; double cpu1,cpu2,cpudiff;
time1 = DateTime.Now; cpu1 = proc.TotalProcessorTime.TotalMilliseconds;
Thread.Sleep(100); proc.Refresh();
time2 = DateTime.Now; cpu2 = proc.TotalProcessorTime.TotalMilliseconds;
cpudiff = cpu2 - cpu1; timediff = time2 - time1;
return Convert.ToInt32(100 / timediff.TotalMilliseconds *
cpudiff); }
I get wrong values...perhaps the Idle process has an average cpu usage of 2950% ????
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% ???? > >
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% ???? > >
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% ???? > > > > > >
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% ???? > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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% ???? > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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? ...
|
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...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |