user-0 提问: 程序如何定时关闭？

回复:
user: user-0
content: 就一个win32控制台程序

user: user-1
content: 这是windows下的编程知识，可以看看win32编程。

user: user-2
content: 试试shutdown /?

user: user-3
content: timer 控件

user: user-4
content: 时间错了……   #include <thread> #include <chrono> #include <atomic>  int main() {     std::atomic<bool> exiting = false;     std::thread timer([&exiting](){           std::this_thread::sleep_for(std::chrono::minutes(40)); exiting = true; });          while (!exiting)     {         // do something     }      timer.join();     return 0; }

user: user-4
content: #include <thread> #include <chrono> #include <atomic>  int main() {     std::atomic<bool> exiting = false;     std::thread timer([&exiting](){  std::this_thread::sleep_for(std::chrono::seconds(10)); exiting = true; });          while (!exiting)     {         // do something     }      timer.join();     return 0; }

user: user-5
content: gettime() 多简单

user: user-6
content: win32console可以设置消息队列，使用 SetTimer(HWND,UNIT,UINT,TIMERPROC) 这个api来实现定时功能

user: user-7
content: 定时器没玩过试试，或者线程每分钟轮询一次呗

user: user-8
content: long run_time = 40*60*1000;         clock_t begin,end; 	begin = clock(); 	end = clock(); 	while(end-begin<run_time) 	{ 	    ...             end = clock(); 	}

user: user-9
content: 仅供参考#pragma comment(lib,"user32") #include <stdio.h> #include <time.h> #include <sys/timeb.h> #include <windows.h> char datestr[16]; char timestr[16]; char mss[4]; void log(char *s) {     struct tm *now;     struct timeb tb;      ftime(&tb);     now=localtime(&tb.time);     sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);     sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );     sprintf(mss,"%03d",tb.millitm);     printf("%s %s.%s %s",datestr,timestr,mss,s); } VOID CALLBACK myTimerProc1(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc1\n"); } VOID CALLBACK myTimerProc2(   HWND hwnd, // handle of window for timer messages   UINT uMsg, // WM_TIMER message   UINT idEvent, // timer identifier   DWORD dwTime // current system time ) {  log("In myTimerProc2\n"); } int main() {     int i;     MSG msg;      SetTimer(NULL,0,1000,myTimerProc1);     SetTimer(NULL,0,2000,myTimerProc2);     for (i=0;i<20;i++) {         Sleep(500);         log("In main\n");         if (GetMessage(&msg,NULL,0,0)) {             TranslateMessage(&msg);             DispatchMessage(&msg);         }      }     return 0; } //2012-07-26 17:29:06.375 In main //2012-07-26 17:29:06.875 In myTimerProc1 //2012-07-26 17:29:07.375 In main //2012-07-26 17:29:07.875 In myTimerProc2 //2012-07-26 17:29:08.375 In main //2012-07-26 17:29:08.375 In myTimerProc1 //2012-07-26 17:29:08.875 In main //2012-07-26 17:29:08.875 In myTimerProc1 //2012-07-26 17:29:09.375 In main //2012-07-26 17:29:09.890 In myTimerProc2 //2012-07-26 17:29:10.390 In main //2012-07-26 17:29:10.390 In myTimerProc1 //2012-07-26 17:29:10.890 In main //2012-07-26 17:29:10.890 In myTimerProc1 //2012-07-26 17:29:11.390 In main //2012-07-26 17:29:11.890 In myTimerProc2 //2012-07-26 17:29:12.390 In main //2012-07-26 17:29:12.390 In myTimerProc1 //2012-07-26 17:29:12.890 In main //2012-07-26 17:29:12.890 In myTimerProc1 //2012-07-26 17:29:13.390 In main //2012-07-26 17:29:13.890 In myTimerProc2 //2012-07-26 17:29:14.390 In main //2012-07-26 17:29:14.390 In myTimerProc1 //2012-07-26 17:29:14.890 In main //2012-07-26 17:29:14.890 In myTimerProc1 //2012-07-26 17:29:15.390 In main //2012-07-26 17:29:15.890 In myTimerProc2 //2012-07-26 17:29:16.390 In main //2012-07-26 17:29:16.390 In myTimerProc1 //2012-07-26 17:29:16.890 In main //2012-07-26 17:29:16.890 In myTimerProc1 //2012-07-26 17:29:17.390 In main //2012-07-26 17:29:17.890 In myTimerProc2 //2012-07-26 17:29:18.390 In main //2012-07-26 17:29:18.390 In myTimerProc1 //2012-07-26 17:29:18.890 In main //2012-07-26 17:29:18.890 In myTimerProc1 //2012-07-26 17:29:19.390 In main //2012-07-26 17:29:19.890 In myTimerProc2

user: user-10
content: 定时器，或者开启线程专门计时（通过开始运行时记录一个点，然后在循环中不停的检测当前点和之前那个点的值，如果超过40*60*1000就退出程序）

user: user-11
content: 1 用alarm设置时间+捕获信号中断 2 开一个监控线程计时 3 主循环计时也是可以的，不过可能不精确