Loop in a dialog which stops when button pressed

Loop in a dialog which stops when button pressed

Post by Jianfeng Ji » Sat, 05 Jun 1993 08:43:52



Does anyone know how to implemented a loop in a dialog procedure which will
stop when the user presses a button via the mouse? In a dialog I have a long
job to process or sometimes to wait for an external signal via I/O. I want
the user to be able to interrupt the waiting loop.

I have tried the following:

static int condition;

case WM_COMMAND:
        switch(wParam)
          {
          case ID_RUN:          // Run button pressed
               if (condition) break;
               do something;
               PostMessage(hDlg,WM_COMMAND,ID_RUN,0L);  // initiate next round
               break;
          case ID_CANCEL:
               condition = TRUE;
               break;
         }

The idea here is to let the loop through the Windows message translation
so the message of the CANCEL button can be processed, and the loop broken.
However, Windows seems never to process the ID_CANCEL message and the program
hangs in the endless loop of processing and re-processing ID_RUN. It
appeared that the message posted by PostMessage always gets through the message
loop first, before any user inputs.

Any suggestions welcome.
Thanks in advance

 
 
 

Loop in a dialog which stops when button pressed

Post by David Braba » Sat, 05 Jun 1993 15:42:39




>Does anyone know how to implemented a loop in a dialog procedure which will
>stop when the user presses a button via the mouse? In a dialog I have a long
>job to process or sometimes to wait for an external signal via I/O. I want
>the user to be able to interrupt the waiting loop.

>I have tried the following:

>static int condition;

>case WM_COMMAND:
>    switch(wParam)
>      {
>      case ID_RUN:          // Run button pressed
>           if (condition) break;
>           do something;
>           PostMessage(hDlg,WM_COMMAND,ID_RUN,0L);  // initiate next round
>           break;
>      case ID_CANCEL:
>           condition = TRUE;
>           break;
>         }

>The idea here is to let the loop through the Windows message translation
>so the message of the CANCEL button can be processed, and the loop broken.
>However, Windows seems never to process the ID_CANCEL message and the program
>hangs in the endless loop of processing and re-processing ID_RUN. It
>appeared that the message posted by PostMessage always gets through the message
>loop first, before any user inputs.

>Any suggestions welcome.
>Thanks in advance


                I think the correct way to achieve what you want is to
temporarily replace the message loop of your application. Something like

                case ID_RUN :

                        for(;;)
                          {
                            while(!PeekMessage(&msg, wnd, 0, 0, PM_REMOVE))
                                 WaitMessage();

                            if(msg.message == UM_CANCEL || msg.message ==
                                                        UM_JOBTERMINATED)
                              break;

                            // progress in your job
                            // when job ended -> SendMessage(...,
                            // UM_JOBTERMINATED, ...);                    
                            }
                         break;

                  case ID_CANCEL : SendMessage(wnd, UM_CANCEL, 0, 0L); break;

        Hmmh. This is only to give you an idea. Maybe I'm wrong.

        David

      ****************************************************************

      * Siemens Nixdorf           *                                  *
      * Centre Software de Liege  * Phone  :  (041) 20.16.09         *
      * 2, rue des Fories         *                                  *
      * 4020 Liege, Belgium       *                                  *
      ****************************************************************

 
 
 

Loop in a dialog which stops when button pressed

Post by Arnoud Marte » Sat, 05 Jun 1993 19:23:19



> Does anyone know how to implemented a loop in a dialog procedure which will
> stop when the user presses a button via the mouse? In a dialog I have a long
> job to process or sometimes to wait for an external signal via I/O. I want
> the user to be able to interrupt the waiting loop.

> I have tried the following:

> static int condition;

> case WM_COMMAND:
>    switch(wParam)
>      {
>      case ID_RUN:          // Run button pressed
>           if (condition) break;
>           do something;
>           PostMessage(hDlg,WM_COMMAND,ID_RUN,0L);  // initiate next round
>           break;
>      case ID_CANCEL:
>           condition = TRUE;
>           break;
>          }

Don't know why this doesn't work, but I think it has something to do with
your message queue. I would use some code like this.

static BOOL bCont = TRUE;

case WM_COMMAND:
        switch(wParam){
          case ID_RUN:
                Do_Something(....);
                break;
          case ID_CANCEL:
                bCont = FALSE;
                break;

void Do_Something(....){
        while(callbackproc){
                // do small amount of work
        }
        return;

Quote:}      

BOOL callbackproc(){
        return bCont;

Quote:}

In this way you do the work without blocking windows and without using
the message queue. Of course you could provide a MakeProcInstance for the
callbackproc and give it as a parameter to the function Do_Something.

The more times you call callbackproc() in Do_Something() the faster the user
will see any action.

Give it a try.

+----------------------------------------------------------------------+
Arnoud Martens                  voice:  +31-15-786057          
Networktheory Section                   +31-15-563621 ($HOME)          
Delft University of Technology                                          

+----------------------------------------------------------------------+