程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> c++控制台程序實現定時器,控制台實現定時器

c++控制台程序實現定時器,控制台實現定時器

編輯:C++入門知識

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

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved