一个监控系统时间,定时自动关机程序

明亮 posted @ 2011年2月17日 11:35 in 【科学综合】 , 1749 阅读
本文发表于:http://fml927.is-programmer.com

闲来无事,顺手写了程序监控系统时间,到达设定的时间后,可以自动关机。

下一步,再将它改写为一个可以自己读取配置脚本文件程序,就可以任意添加定时任务,这样就可以自己完成一个定时提醒功能的工具了!

定时关机的小工具代码列表如下,用VS2005生成的Windows应用程序,然后删除界面相关的无关代码,添加自己的代码,修改了头文件即可。

编译的结果添加到windows启动项中,即可随机启动。

   1: //shutDownWin32.h
   2:  
   3: #pragma once
   4:  
   5: #include "resource.h"
   6: #include "stdafx.h"
   7: #include "windows.h"
   8: //#include "thread.h"
   9: #include "iostream"
   1: //shutDownWin32.c
   2:  
   3: #include "stdafx.h"
   4: #include "shutDownWin32.h"
   5:  
   6: //保存时间的变量
   7: _SYSTEMTIME LocalTime ;
   8: LPSYSTEMTIME lpLocalTime = &LocalTime;
   9: bool  bWork = false ;
  10:  
  11: int APIENTRY _tWinMain(HINSTANCE hInstance,
  12:                      HINSTANCE hPrevInstance,
  13:                      LPTSTR    lpCmdLine,
  14:                      int       nCmdShow)
  15: {
  16:     //程序一直监控
  17:     while(1)
  18:     {
  19:         //获取系统时间 windows API
  20:         GetLocalTime( lpLocalTime );
  21:  
  22:         //21:00后就自动关机
  23:         if( (lpLocalTime->wHour>=21) && !bWork  )
  24:         {
  25:             //利用系统工具实现,60s后关机
  26:             system("shutdown -s -t 60");
  27:             bWork = true ;
  28:         }
  29:  
  30:         //间隔一秒后 再次读取系统时间 减少系统资源占用
  31:         Sleep(1000);
  32:     }
  33:  
  34:     return 0;
  35: }
  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter