Click here to Skip to main content
15,911,360 members
Articles / Operating Systems / Windows
Article

Take user's action within "loop forever"

Rate me:
Please Sign up or sign in to vote.
1.42/5 (32 votes)
25 Dec 20021 min read 85.4K   18   21
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


    Written By
    Web Developer
    Hong Kong Hong Kong
    Still alive

    Comments and Discussions

     
    Generalcapture message Pin
    Tailana1-Jun-05 22:29
    Tailana1-Jun-05 22:29 
    GeneralRe: Much ado about nothing... Pin
    zarzor14-Dec-02 16:12
    zarzor14-Dec-02 16:12 
    GeneralRe: Much ado about nothing... Pin
    jung-kreidler15-Dec-02 20:35
    jung-kreidler15-Dec-02 20:35 
    GeneralRe: Much ado about nothing... Pin
    KingSharon16-Dec-02 14:02
    KingSharon16-Dec-02 14:02 
    GeneralI gave you 5 b/c Pin
    Rocky1010-Dec-02 3:46
    Rocky1010-Dec-02 3:46 
    GeneralTo all the nay sayers Pin
    Tim Smith9-Dec-02 4:51
    Tim Smith9-Dec-02 4:51 
    GeneralRe: To all the nay sayers Pin
    Andreas Saurwein9-Dec-02 5:08
    Andreas Saurwein9-Dec-02 5:08 
    GeneralRe: Unusable Pin
    Tim Smith9-Dec-02 4:38
    Tim Smith9-Dec-02 4:38 
    GeneralRe: Unusable Pin
    #realJSOP9-Dec-02 5:07
    professional#realJSOP9-Dec-02 5:07 
    GeneralGames Pin
    Hugo Hallman4-Dec-02 7:47
    Hugo Hallman4-Dec-02 7:47 
    QuestionSorry? Pin
    Andreas Saurwein3-Dec-02 23:50
    Andreas Saurwein3-Dec-02 23:50 
    GeneralRe: Sorry? Pin
    Andreas Saurwein9-Dec-02 5:06
    Andreas Saurwein9-Dec-02 5:06 
    GeneralWhere does this differ.. Pin
    jhwurmbach3-Dec-02 20:47
    jhwurmbach3-Dec-02 20:47 
    GeneralNot that I disagree with that solution Pin
    igor19603-Dec-02 16:33
    igor19603-Dec-02 16:33 
    GeneralRe: Not that I disagree with that solution Pin
    Anonymous4-Dec-02 12:30
    Anonymous4-Dec-02 12:30 
    GeneralOr you could... Pin
    Klaus Probst3-Dec-02 13:30
    Klaus Probst3-Dec-02 13:30 
    ... use Visual Basic, which wraps all this in a simple statement called DoEvents Poke tongue | ;-P

    ___________
    Klaus
    [vbbox.com]
    GeneralRe: Or you could... Pin
    Jubjub3-Dec-02 14:54
    sussJubjub3-Dec-02 14:54 
    GeneralRe: Or you could... Pin
    Nitron4-Dec-02 3:38
    Nitron4-Dec-02 3:38 
    GeneralThe correct way to do this Pin
    Anonymous3-Dec-02 13:21
    Anonymous3-Dec-02 13:21 
    GeneralRe: The correct way to do this Pin
    Thomas Freudenberg3-Dec-02 14:24
    Thomas Freudenberg3-Dec-02 14:24 
    GeneralYou should Pin
    Jörgen Sigvardsson3-Dec-02 12:42
    Jörgen Sigvardsson3-Dec-02 12:42 

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

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