4

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?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99

2 Answers2

4

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
  • 53,498
  • 9
  • 91
  • 140
  • 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 Oct 01 '10 at 23:18
  • 1
    @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 Oct 02 '10 at 01:03
1

Don't invent hot water. Use WMI.

Vnuk
  • 2,673
  • 1
  • 32
  • 49