c++控制台程序实现定时器

点击打开链接
#include "stdafx.h"  #include <iostream>  #include <Windows.h>    using namespace std;    void CALLBACK TimeProc(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime);      int _tmain(int argc, _TCHAR* argv[])  {      SetTimer(NULL,1,1000,TimeProc);      MSG msg;      while (GetMessage(&msg,NULL,0,0))      {          if (msg.message == WM_TIMER)          {              DispatchMessage(&msg);          }      }      return 0;  }  int ncount = 0;  void CALLBACK TimeProc(HWND hwnd,UINT message,UINT idTimer,DWORD dwTime)  {      cout<<ncount++<<endl;  }  /* 何问起 hovertree.com */
#include "stdafx.h"  #include <ctime>  #include <Windows.h>  #include <conio.h>  #include <stdio.h>      int ncount = 0;    void CALLBACK TimerProc(HWND hWnd,UINT nMsg,UINT_PTR idEvent,DWORD dwTime)  {      ncount++;    }    DWORD CALLBACK Thread(PVOID pvoid)  {      MSG msg;      PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);      UINT timerid = SetTimer(NULL,1,10000,TimerProc);      BOOL bRet;      while ((bRet = GetMessage(&msg,NULL,0,0)) != 0)      {          if (bRet == -1)          {              printf("Error:the thread will quit,error id is %d/n",GetLastError());              break;          }          else          {              TranslateMessage(&msg);              DispatchMessage(&msg);          }      }      KillTimer(NULL,timerid);      printf("thread end here/n");      return 0;    }    int _tmain(int argc, _TCHAR* argv[])  {        HANDLE hThread = CreateThread(NULL,0,Thread,NULL,0,NULL);      _getch();      return 0;  } /* 何问起 hovertree.com */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值