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

推荐:http://www.cnblogs.com/roucheng/p/cppjy.html

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

#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 */

推荐:http://www.cnblogs.com/roucheng/p/cppjy.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2021年12月25日 下午1:00
下一篇 2021年12月25日 下午1:00


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号