Click here to Skip to main content
Licence 
First Posted 2 Dec 2002
Views 66,138
Bookmarked 18 times

Take user's action within "loop forever"

By | 25 Dec 2002 | Article
How to take user's action when your program is busy.

Introduction

One of the problems encountered by the programmer is how to consider the user action when activated (say, user pushed stop-button control) during continuous operation. In fact, this has been solved in the past by inserting the loop (as function) in the primary-message loop and replacing GetMessage() by PeekMessage() in WinMain(), as follows:

while(TRUE)
{
    if(PeekMessage(&msg,0,0,0,PM_REMOVE))
    {
        if(msg.message==WM_QUIT) break;
        DispatchMessage(&msg);
    }
    else LoopFun();
}

Also OnIdle() operates in the same manner. This function will be blocked if there are many messages sent to the app. Another way is to create a thread, but there you have to pay attention about thread safety, because your thread runs in a different context (thanks to jung-kreidler, CP member).

Best Solution

Consider you have a program doing continuous processing, and you want to permit the user to stop this process at any time he/she wishes. At first glance, this is impossible, because window messages are not posted when the program is busy. But inserting the DispatchMessage() API function within the loop can solve this problem as follows:

bool bProgress; //global var or volatile 
     //(Thanks to Andreas Saurwein,CP member) 
....
LRESULT CALLBACK MainWndProc(HWND hwnd, 
   UINT umsg, WPARAM wParam, LPARAM lParam)
{
    switch (umsg){
        .....
        case WM_COMMAND:
        switch (LOWORD(wParam)){
            case IDC_LOOP:
            {
       
                bProgress=TRUE;
                //loop around a time consuming process
                while(bProgress) 
                {
                    .... //process body
                    //check if user stop the process
                    while(PeekMessage(&msg,0,0,0,PM_REMOVE))
                        DispatchMessage(&msg);
                }
                break;
            }
            ....
            case IDC_STOP:
                bProgress=FALSE;
                break;
                ....

So, having secondary message loops is a common practice... this is how modal dialogs are done. Also, in many cases such as popup windows or tracking, secondary message loops are executed while capture remains set to the expected window (thanks to Tim Smith, CP member).

In addition to this technique, the beginner will learn from the complete code, the following things:

  1. The generic programming for Win32 API,
  2. How to create & use some of the standard controls directly without resource editor.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    zarzor

    Web Developer

    Hong Kong Hong Kong

    Member

    Still alive

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Generalcapture message PinmemberTailana22:29 1 Jun '05  
    GeneralRe: Much ado about nothing... Pinmemberzarzor16:12 14 Dec '02  
    GeneralRe: Much ado about nothing... Pinsussjung-kreidler20:35 15 Dec '02  
    GeneralRe: Much ado about nothing... PinmemberKingSharon14:02 16 Dec '02  
    GeneralI gave you 5 b/c PinmemberRocky103:46 10 Dec '02  
    GeneralTo all the nay sayers PinmemberTim Smith4:51 9 Dec '02  
    GeneralRe: To all the nay sayers PinmemberAndreas Saurwein5:08 9 Dec '02  
    GeneralRe: Unusable PinmemberTim Smith4:38 9 Dec '02  
    GeneralRe: Unusable PinmemberJohn Simmons / outlaw programmer5:07 9 Dec '02  
    GeneralGames PinmemberHugo Hallman7:47 4 Dec '02  
    QuestionSorry? PinmemberAndreas Saurwein23:50 3 Dec '02  
    GeneralRe: Sorry? PinmemberAndreas Saurwein5:06 9 Dec '02  
    GeneralWhere does this differ.. Pinmemberjhwurmbach20:47 3 Dec '02  
    GeneralNot that I disagree with that solution Pinmemberigor196016:33 3 Dec '02  
    GeneralRe: Not that I disagree with that solution PinsussAnonymous12:30 4 Dec '02  
    GeneralOr you could... PinmemberKlaus Probst13:30 3 Dec '02  
    GeneralRe: Or you could... PinsussJubjub14:54 3 Dec '02  
    GeneralRe: Or you could... PinmemberNitron3:38 4 Dec '02  
    GeneralThe correct way to do this PinsussAnonymous13:21 3 Dec '02  
    GeneralRe: The correct way to do this PinmemberThomas Freudenberg14:24 3 Dec '02  
    GeneralYou should PinmemberJörgen Sigvardsson12:42 3 Dec '02  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120517.1 | Last Updated 26 Dec 2002
    Article Copyright 2002 by zarzor
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid