views:

91

answers:

2

Is there a way (.net 2.0) for a process to know EXACT amount of processor usage it consumes (something like it would be seen in processexplorer properties/performance graph).

Please, don't go much further then semi-esoteric questions (p-Invoke OK)

Also, so I don't open yet another question - how can I (as a process) control my own affinity - i.e. on which processors (cores) should I be allowed to run?

+4  A: 

Process affinity is set using Process.ProcessorAffinity.

You can get Process CPU time using Process.TotalProcessorTime but I don't know if this gives you the desired granularity and snapshotting ability.

Steve Townsend
I guess that's the answer - I just need to measure realtime and divide TotalProcessorTime with it to get my percentage. Thx.
Daniel Mošmondor
@Daniel - yes, you should be able to calculate the delta since last measurement and use that to display something representative. I imagine native code offers better precision though (could be wrong, and f so I will get flamed for this heresy).
Steve Townsend
+1  A: 

Don't invent hot water. Use WMI.

Vnuk