Implementation of timer function in MFC DLL

Source: Internet
Author: User
Tags getmessage

Method One:
Create a thread and read the system time over and over again? If the timing requirements are not strict, with sleep can be.
DWORD WINAPI TimerThread (lpvoid pamaram) { UINT oldtickcount, Newtickcount; Oldtickcount = GetTickCount (); Gets the number of milliseconds while (true) { while (true) { newtickcount = GetTickCount (); Gets the number of milliseconds if (newtickcount-oldtickcount >= 60000) { oldtickcount = Newtickcount; break; } else switchtothread (); } 60 milliseconds Auto Call function somefun (); } return 0; }

  

Method Two:  the thread function in the DLL can be used like this using the timerUINT ThreadProc (LPVOID) {SetTimer (null, 1, n, null); MSG msg;//PeekMessage forces the system to establish a message stack peekmessage (&msg, NULL, NULL, NULL, FALSE) for the thread, while (GetMessage (&msg, NULL, NULL, NULL)) {switch (msg.message) {case wm_timer:{//is executed once every 5 seconds}break;} TranslateMessage (&msg);//dispatchmessage (&msg);} KillTimer (NULL, 1); return 0;}

  

DWORD Tidtimer; UINT Uelapse; unsigned __stdcall timerthreadproc (void *param) {    MSG msg;    Uint_ptr Htimer;     if (Htimer = SetTimer (null, NULL, UELAPSE, 0)) = = 0)        MessageBox (null, L "Set Timer failed.", L "Error", MB_OK);    while (GetMessage (&msg, NULL, 0, 0))    {        switch (msg.message)        {case        Wm_timer:            //...            break;         Default: Break            ;        }    }    if (Htimer)        KillTimer (NULL, htimer);    return 0;} _beginthreadex (null, 0, timerthreadproc, NULL, 0, &tidtimer); ... PostThreadMessage (Tidtimer, wm_timer, 0, 0); A subsequent event handler starts the timer

  

Implementation of timer function in MFC DLL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.