The answers above give a pretty good example for a way to calculate CPU usage.But I would like to point out something that is incorrect and was misled. The SubtractTimes function needs to be slightly different.
private UInt64 SubtractTimes(ComTypes.FILETIME a, ComTypes.FILETIME b)
{
UInt64 aInt = ((UInt64)(a.dwHighDateTime << 32)) | (UInt32)a.dwLowDateTime;
UInt64 bInt = ((UInt64)(b.dwHighDateTime << 32)) | (UInt32)b.dwLowDateTime;
return aInt - bInt;
}
NOTICE THE UINT32 for the lowDateTime. This is because, atleast in C#, the comtype.FILETIME struct casts the DWORD to an int32, so if the value is more than MAX signed Int you see a negative value which should never be the case. If you cast this directly to "UINT64" type, internally for preserving sign this is converted to Int64 first and then converted to UInt64 so you see an incorrect value. Instead You want to cast the lowDateTime to a Uint32 type to get rid of the negative sign, and then cast to UInt64 (this would happen automatically since it is part of | operation). Same should hold for the dwHighDateTime conponent but thats fine since it should not be more than MaxInt usually (well, that again depends on your usecase).