ProjectBLL/CpuUsage.cs
/// <summary> 
/// Determines the current average CPU load. 
/// </summary> 
/// <returns>An integer that holds the CPU load percentage.</returns> 
/// <exception cref="NotSupportedException">One of the system calls fails. The CPU time can not be obtained.</exception> 
Determines the current average CPU load.
Determines the current average CPU load .
public override int Query()
        {
            byte[] timeInfo = new byte[32];   
            byte[] perfInfo = new byte[312]; 
            double dbIdleTime, dbSystemTime;
            int ret;
            ret = NtQuerySystemInformation(SYSTEM_TIMEINFORMATION, timeInfo, timeInfo.Length, IntPtr.Zero);
            if (ret != NO_ERROR)
                throw new NotSupportedException();
            ret = NtQuerySystemInformation(SYSTEM_PERFORMANCEINFORMATION, perfInfo, perfInfo.Length, IntPtr.Zero);
            if (ret != NO_ERROR)
                throw new NotSupportedException();
            dbIdleTime = BitConverter.ToInt64(perfInfo, 0) - oldIdleTime;
            dbSystemTime = BitConverter.ToInt64(timeInfo, 8) - oldSystemTime;
            if (dbSystemTime != 0)
                dbIdleTime = dbIdleTime / dbSystemTime;
            dbIdleTime = 100.0 - dbIdleTime * 100.0 / processorCount + 0.5;
            oldIdleTime = BitConverter.ToInt64(perfInfo, 0); 
            oldSystemTime = BitConverter.ToInt64(timeInfo, 8); 
            return (int)dbIdleTime;
        }
public override int Query ( ) { byte [ ] timeInfo = new byte [ 32 ] ; byte [ ] perfInfo = new byte [ 312 ] ; double dbIdleTime , dbSystemTime ; int ret ; ret = NtQuerySystemInformation ( SYSTEM_TIMEINFORMATION , timeInfo , timeInfo . Length , IntPtr . Zero ) ; if ( ret != NO_ERROR ) throw new NotSupportedException ( ) ; ret = NtQuerySystemInformation ( SYSTEM_PERFORMANCEINFORMATION , perfInfo , perfInfo . Length , IntPtr . Zero ) ; if ( ret != NO_ERROR ) throw new NotSupportedException ( ) ; dbIdleTime = BitConverter . ToInt64 ( perfInfo , 0 ) - oldIdleTime ; dbSystemTime = BitConverter . ToInt64 ( timeInfo , 8 ) - oldSystemTime ; if ( dbSystemTime != 0 ) dbIdleTime = dbIdleTime / dbSystemTime ; dbIdleTime = 100.0 - dbIdleTime * 100.0 / processorCount + 0.5 ; oldIdleTime = BitConverter . ToInt64 ( perfInfo , 0 ) ; oldSystemTime = BitConverter . ToInt64 ( timeInfo , 8 ) ; return ( int ) dbIdleTime ; }
Determines the current average CPU load.
Determines the current average CPU load .
// SYSTEM_TIME_INFORMATION structure  // SYSTEM_PERFORMANCE_INFORMATION structure  // get new system time  // get new CPU's idle time  // CurrentValue = NewValue - OldValue  // CurrentCpuIdle = IdleTime / SystemTime  // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors  // store new CPU's idle and system time  // SYSTEM_PERFORMANCE_INFORMATION.liIdleTime  // SYSTEM_TIME_INFORMATION.liKeSystemTime 
An integer that holds the CPU load percentage.
An integer that holds the CPU load percentage .
One of the system calls fails. The CPU time can not be obtained.
One of the system calls fails . The CPU time can not be obtained .