分类: C/C++
2008-03-14 14:21:19
void CAutoShutDownDlg::GetCpuUsage() { // get system time status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0); if (status!=NO_ERROR) { MessageBox("failed to get system time!"); } // get cpu idle time status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL); if (status != NO_ERROR) { MessageBox("failed to get cpu time"); } if (liOldIdleTime.QuadPart != 0) { // new cpu Time = NewTime - OldTime dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime); dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime); //get cpu usage unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5; } wsprintf(CpuUsage,"%d%%",(UINT)unCpuUsage); m_sCpuUsage=(CString)CpuUsage; //if cpu usage ==0% then shutdown os if(bSetAuto&&(UINT)dbCpuIdleTime==0) { //MessageBox("shutdown os!"); ShutDown(); } // store new cpu idle and system time liOldIdleTime = SysPerfInfo.liIdleTime; liOldSysTime = SysTimeInfo.liKeSystemTime; }在定时器里每秒得到CPU的利用率,如果为0关闭系统。关机参数设置如下:
ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE|EWX_POWEROFF,NULL);注意此处我设为强制关机,所以注意要保存当前数据,在matlab里训练完立即保存数据。也许这样的情况大家很少能遇到,我这种方法很简单很直接,希望广大读者给出好的方法或建议。