如题,如何在控制台程序中生成定时器?如果用settimer的话好像有点问题,最后一个参数是回调函数,这个回调函数如何定义?还有没有其他生成定时器的方法,望告知。
谢谢!

解决方案 »

  1.   

    user timer in workthread of console app
    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    unsigned long WINAPI Thread(PVOID pvoid);
    void main()
    {
    DWORD dwThreadId;
    printf("use timer in workthread of console application<masterz>\n");
        HANDLE hThread = CreateThread( 
            NULL,                        // no security attributes 
            0,                           // use default stack size  
            Thread,                  // thread function 
            0,                // argument to thread function 
            0,                           // use default creation flags 
            &dwThreadId); 
    DWORD dwwait=WaitForSingleObject(hThread,1000*30);
    switch(dwwait)
    {
    case WAIT_ABANDONED:
    printf("main thread WaitForSingleObject return WAIT_ABANDONED\n");
    break;
    case WAIT_OBJECT_0:
    printf("main thread WaitForSingleObject return WAIT_OBJECT_0\n");
    break;
    case WAIT_TIMEOUT:
    printf("main thread WaitForSingleObject return WAIT_TIMEOUT\n");
    break;
    }
    CloseHandle(hThread);
    _getch();
    }unsigned long WINAPI Thread(PVOID pvoid)
    {
     MSG msg;
         PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
     UINT timerid=SetTimer(NULL,111,3000,NULL);
         BOOL bRet;
    int count =0;
    while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)

    if (bRet == -1)
    {
    // handle the error and possibly exit
    }
    else
    if(msg.message==WM_TIMER)
    {
    count++;
    printf("WM_TIMER in work thread count=%d\n",count);
    if(count>4)
    break;
    }
    else
    {
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    }
    }
    KillTimer(NULL,timerid);
    printf("thread end here\n");
    return 0;
    }
      

  2.   

    you can also use multiple waitable timer
    下面的代码在VC2003中执行,线程会等待10秒
    #define _WIN32_WINNT 0x0500#include <windows.h>
    #include <conio.h>
    #include <iomanip>
    #include <iostream>
    #include <process.h>
    #include <sstream>
    using namespace std;
    typedef unsigned (__stdcall *PTHREAD_START) (void *);
    #define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, \
    pvParam, fdwCreate, pdwThreadId) \
    ((HANDLE)_beginthreadex( \
    (void *) (psa), \
    (unsigned) (cbStack), \
    (PTHREAD_START) (pfnStartAddr), \
    (void *) (pvParam), \
    (unsigned) (fdwCreate), \
    (unsigned *) (pdwThreadId)))
    DWORD WINAPI _ThreadProc(PVOID pvParam);
    HANDLE TimeHandle;
    void print_now(LPCTSTR msg)
    {
    std::stringstream ss;
    SYSTEMTIME st;
    GetLocalTime(&st);
    ss<<msg<<" "<<st.wHour<<":"<<st.wMinute<<":"<<st.wSecond<<"."<<st.wMilliseconds<<endl;
    cout<<ss.str();
    }
    int main()
    {
    SYSTEMTIME stm;
    FILETIME ftLocal,ftUTC;
    LARGE_INTEGER liUTC;
    HANDLE ProcHandle;
    DWORD ThreadID;liUTC.QuadPart=-100000000;
    TimeHandle = CreateWaitableTimer(NULL,TRUE,NULL);
    print_now(__FUNCTION__);
    cout<<"等待10秒....."<<endl;cout<<"CreateWaitableTimer error code "<<GetLastError()<<std::endl;
    SetWaitableTimer(TimeHandle,&liUTC,0,NULL,NULL,0);
    cout<<"SetWaitableTimer error code "<<GetLastError()<<std::endl;ProcHandle=chBEGINTHREADEX(NULL,0,_ThreadProc,NULL,0,&ThreadID);
    //CloseHandle(TimeHandle); // this call will activate TimeHandle
    CloseHandle(ProcHandle);
    cout<<"sleep a minute"<<endl;
    print_now(__FUNCTION__);
    //Sleep(1*60*1000);
    system("pause");}
    DWORD WINAPI _ThreadProc(PVOID pvParam)
    {
    print_now(__FUNCTION__);
    DWORD dw_wait = WaitForSingleObject(TimeHandle,INFINITE);
    cout<<__FUNCTION__<<" wait return "<<dw_wait<<endl;
    cout<<"报时 "<<endl;
    print_now(__FUNCTION__);
    return 0;
    }
      

  3.   

    定时器本来就是回调函数。MFC下的OnTimer()其实就是一个回调函数。你将需要在OnTimer中要做的工作写成一个全局函数,然后将该函数作为定时器的最后一个回调函数参数即可。