dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: WM_TIMER message

  1. #1
    Join Date
    Sep 2019
    Posts
    11

    WM_TIMER message's behavior

    Hello,

    I have a simple question regarding the WM_TIMER message. As I understand, messages for a certain Window Procedure are run one by one, as it is said in the book Programming Windows 5th ed. by Charles Petzold at page 55 (section Queued and Nonqueued Messages)


    Regarding the WM_TIMER message, it seems that it is an exception..if I have

    SetTimer(hWnd, 3, 500, NULL);

    at the end of the WM_CREATE message in a window procedure, so I set up the timer with Id 3 that is to be run each 0.5 seconds, and if

    WM_TIMER:
    if (wParam == 3)
    {
    MessageBox(hWnd, TEXT("3"), NULL, MB_OK);
    }
    break;

    then it seems that processing a WM_TIMER message for timer 3 is interrupted by the next WM_TIMER message for timer 3..and so on..


    Is WM_TIMER special, and if yes, are there also other special messages?


    Waiting for your valuable reply,
    Best,
    D Narcis
    Last edited by dnarcistrinca; January 15th, 2020 at 08:30 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: WM_TIMER message

    The problem isn't with the message per se, the problem is the message box is modal amd prevents the main window of the app from being able to process messages.

    To test this, add the MessageBox(...) code to another message type and you'll see the same 'blocking' behavior.

  3. #3
    Join Date
    Sep 2019
    Posts
    11

    Re: WM_TIMER message

    Quote Originally Posted by Arjay View Post
    The problem isn't with the message per se, the problem is the message box is modal amd prevents the main window of the app from being able to process messages.

    To test this, add the MessageBox(...) code to another message type and you'll see the same 'blocking' behavior.
    Ok, but how can the system send a new message if the previous one has not been processed yet (previous message still running at MessageBox until the message box is not closed by user..) ?

    In the book, it is saying at page 56 that "When a message loop retrieves a message from its message queue and calls DispatchMessage to send the message off to the window procedure, DispatchMessage does not return until the window procedure has returned control back to Windows."

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: WM_TIMER message

    Maybe I don't understand what you are getting at in your belief that WM_TIMER is special?

    To me, the rule is that the main UI thread should never be blocked amd should always be appowed to process messages. In other words, while your program runs, it should never appear in task manager as "not responding". If it does, it means the main ui thread has been blocked for more than 2 seconds.

    While learning Windows programming use something other than MessageBox as a tracing mechanisn. Depending on your dev environment there may be debug outputs you can use like the TRACE macro or DebugOutputString (or is it OutputDebugString?). At any rate, use a non-blocking tracing mechanism to see the actual messaging behavior.

    Also, read the section on modal dialogs in Petzold's book.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: WM_TIMER message

    WM_TIMER is special in that it is a low-priority queued message. These are kept in the queue and are forwarded to the window procedure only when the queue contains no other messages (the same for WM_QUIT & WM_PAINT). This means that the timer interval is not accurate. Messages are generated by Windows in response to an event - pressing a button, moving the cursor, clicking the mouse etc etc. WM_TIMER is also special in that this message is triggered when an internal timer event occurs - and not an external event. The internal timer will repeatedly trigger after the specified interval.

    See https://docs.microsoft.com/en-us/win...message-queues

    It's OutputDebugString() https://docs.microsoft.com/en-us/win...utdebugstringa which displays the message to the attached debugger. It can be easier to create a console and display debug info on that - or to output debug info to a file.
    Last edited by 2kaud; January 15th, 2020 at 12:59 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  6. #6
    Join Date
    Sep 2019
    Posts
    11

    Re: WM_TIMER message

    Quote Originally Posted by 2kaud View Post
    WM_TIMER is special in that it is a low-priority queued message. These are kept in the queue and are forwarded to the window procedure only when the queue contains no other messages (the same for WM_QUIT & WM_PAINT). This means that the timer interval is not accurate. Messages are generated by Windows in response to an event - pressing a button, moving the cursor, clicking the mouse etc etc. WM_TIMER is also special in that this message is triggered when an internal timer event occurs - and not an external event. The internal timer will repeatedly trigger after the specified interval.

    See https://docs.microsoft.com/en-us/win...message-queues

    It's OutputDebugString() https://docs.microsoft.com/en-us/win...utdebugstringa which displays the message to the attached debugger. It can be easier to create a console and display debug info on that - or to output debug info to a file.
    Hello,

    yes, something happens when modal dialog boxes are used..

    I tested again, this time using the Sleep function and it seems that WM_TIMER messages for a certain timer are now ignored while a previous WM_TIMER message for that timer is still running..

    Many appreciations for the help..

    Best,
    D Narcis

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: WM_TIMER message

    You never want to block the UI thread from processing messages. That is a basic rule for Windows programming. Using a message box or sleep inside the message pump blocks the ui thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)