c语言sscanf函数的用法是什么
299
2022-09-01
利用控制台下定时器定时关机代码示例
定时关机,用srvany安装成服务使用。
示例代码工程,基于vs2010
/Files/alantop/shutoff.rar
// shutoff.cpp : 定义控制台应用程序的入口点。 // #include " stdafx.h " #include < atltime.h > #include < windows.h > #include < stdio.h > #include < conio.h > void shutdown(){ // 从config中读取关机参数 CString cs = " shutoff " ; CString hour = " hour " ; CString filename = " .\\config.ini " ; int offHour = ::GetPrivateProfileInt(cs,hour, 0 ,filename); CString min = " min " ; int offMin = ::GetPrivateProfileInt(cs,min, 0 ,filename); // 判断时间并关机 CTime t = CTime::GetCurrentTime(); CTime t1(t.GetYear(),t.GetMonth(),t.GetDay(),offHour,offMin,t.GetSecond()); CTimeSpan ts = t1 - t; printf( " 还有%d小时%d分钟关机.\n " , ts.GetHours(),ts.GetMinutes() ); int nHour = t.GetHour(); int nMin = t.GetMinute(); char command[] = " c:\\windows\\system32\\shutdown.exe -s -t 60 " ; if (nHour == offHour && nMin == nHour) system(command);}VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime) { // printf("定时器函数开始工作\n"); shutdown();} DWORD CALLBACK Thread(PVOID pvoid) { BOOL bRet; MSG msg; PeekMessage( & msg,NULL,WM_USER,WM_USER,PM_NOREMOVE); UINT timerid = ::SetTimer(NULL, 111 , 10000 ,TimerProc); while ((bRet = GetMessage( & msg,NULL, 0 , 0 )) != 0 ) { if (bRet ==- 1 ) { // handle the error and possibly exit } else { TranslateMessage( & msg); DispatchMessage( & msg); } } KillTimer(NULL,timerid); printf( " 线程函数结束\n " ); return 0 ; } int _tmain( int argc, _TCHAR * argv[]){ CString cs = " shutoff " ; CString hour = " hour " ; CString filename = " .\\config.ini " ; int offHour = ::GetPrivateProfileInt(cs,hour, 0 ,filename); CString min = " min " ; int offMin = ::GetPrivateProfileInt(cs,min, 0 ,filename); DWORD dwThreadId; printf( " 定时关机程序启动,计算机将在%d小时%d关机。\n " ,offHour, offMin ); HANDLE hThread = CreateThread(NULL, 0 ,Thread, 0 , 0 , & dwThreadId); _getch(); return 0 ;}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~