Solved

Sending a "quit" message

Posted on 2002-07-12
6
187 Views
Last Modified: 2013-11-20
I have an endless loop that collects data from a port and displays it, but I have no way for the user to exit the program.

Is there some way to have the user click a button or something and have the loop terminate?  The normal handler functions don't work because execution never leaves my "Get/Display" loop.
0
Comment
Question by:Hep_Cat
6 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7150403
There are couple ways to do that

1. Start worket thread ( Let's say using _beginthread or any other way) and run you loop in that thread calling Sleep(1) in a loop to let main aplication some time for processing.

2. Incorporate messsage pump into you loop

something like
while(1)
{
   MSG msg;

   while( GetMessage( &msg, NULL, 0, 0 ) )
   {
     TranslateMessage( &msg );
     DispatchMessage( &msg );
   }
  / do your stuff here....
}

I like first method.

Hope it helps...
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7150612
@ mblat:

Your code doesn't work: the "do your stuff here..." is called after a WM_QUIT-message has been posted - this is the only case in which GetMessage returns FALSE. I also doubt that this type of message-loop is accessible in an MFC-application. P.S.: The correct use of the Sleep()-call would be: Sleep( 0 ) which passes all remaining time from the thread's time slice to other threads running in the same process.

@ Hep_Cat:

You have two alternatives, an easy-but-dirty-hack or the clean way to go:
* dirty-hack: use
exit( 0 );
and your application will terminate instantly. If you use this on win 9x systems it will get unstable after a while since no memory clean-up is performed and resources aren't freed at program termination.
* Put your Get/Display-loop on a second thread and terminate this thread when the user wants to exit your program. The easiest way to approach this would be to create an event (look up CreateEvent in MSDN). If the user wants to terminate your application put it in signalled state. In your Get/Display-loop put a WaitForSingleObject-call to see whether program termination was requested and act accordingly. After you signalled the event upon user request to terminated wait for your Get/Display-thread to terminate and finally exit the application.
0
 
LVL 4

Expert Comment

by:mblat
ID: 7150893
2 fll0yd - you right it should be

while(1)
{
        if (PeekMessage (&wmsg, NULL, 0, 0, PM_REMOVE))
        {
        if(wmsg.message == WM_QUIT)
            return;
           
        TranslateMessage(&wmsg);
        DispatchMessage(&wmsg);
        }

// do the stuff here....
}

as far as it being correct usage in MFC type apps - while it is not "clean" way - ( an I mentioned that I prefer way with threads ) I've seen it done all over the place.  It works.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:fl0yd
ID: 7151407
This type of message-loop is *CLEAN* coding style and is being used in the majority of games, I'd say. It doesn't work with MFC though and needs to be adjusted:
* the "return;"-statement should be replaced by a "break;" to make the code cleaner, ie process clean-up directly after the 'infinite' loop.
* you need to initialize your wmsg-structure if you want to be safe
* you need to replace "if( PeekMessage( ..." by "while( PeekMessage( ..." otherwise your application can expose awkward behaviour [handling one message at a time and interleaving it with your own -- possibly computationally expensive -- code]

summary:

MSG wmsg = { 0 };
while( true ) {
    while( PeekMessage( &wmsg, NULL, 0, 0, PM_REMOVE ) ) {
        if( WM_QUIT == wmsg.message )
            break;
        TranslateMessage( &wmsg );
        DispatchMessage( &wmsg );
    }
    // do the stuff here...
}
// perform clean-up code

This is the way to go. Might sound like nitpicking and marginal changes, but they certainly have immense impact on the application's behaviour.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 7152066
I think that all Hep_Cat wants is a way to let the user break out of the loop.  Form there, the question of exiting the program takes care of itself (the user can then click the Close button or whatever.


In that case, add a button named "Stop Looping" somewhere.  Make its button handler look like:

void OnStopLooping()
{
   m_fAbort= TRUE;  // m_fAbort is dlg member variable
}

Next add this fn somewher:

void KeepUiActive()
{
     MSG msg;
     while( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) {
          AfxGetApp()->PumpMessage();  
     }
}

And in your endless loop, add code like this:

KeepUiActive();
if (m_fAbort) {
   // ... code here to stop the exit the loop
}

=-=-=--==-=-=-=-=-=-
The message pump lets the framework notice and respond to the clicks of buttons.  It also lets the user drag the window around and other important U/I stuff.  With that happening all you need is a mechanism to break out of the loop when the user has clicked a button.  That's the m_fAbort variable.

-- Dan

0
 

Author Comment

by:Hep_Cat
ID: 7157699
This was the simplest.  And it had the most complete code examples.  Worked very well.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
Separate into files by filename 12 76
Expand data scrubbing tool 13 32
Problem to App 4 40
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now