How to Get CPU Usage Per Thread

Category:
System/API
Type:
Classes
Difficulty:
Intermediate
Author:
Anonymous

Version Compatibility: Visual Basic.NET

More information:
I searched all over the internet, and found little or no help. No one seemed to have any code for this, and it's not so arcane a subject that it should be so hard to find. If you are writing a multi-threaded app, then at some point you're going to want to know how much CPU time each thread is using, so you can optimize it accordingly. This is a little helper class I wrote to handle that problem. You provide the class a native thread id or just Dim it within the thread you want to watch. The CPUtimer.CPUusage variable is a short representing the percentage of CPU used by this thread. I hope this saves someone some time. This took me way too long.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: