前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++控制台程序实现定时器

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

作者头像
全栈程序员站长
发布2022-07-15 16:04:26
5160
发布2022-07-15 16:04:26
举报

大家好,又见面了,我是全栈君

代码语言:javascript
复制
#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 */
代码语言:javascript
复制
#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 */

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120449.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档