Solved

DoEvents in VC++/MFC

Posted on 2001-06-05
4
804 Views
Last Modified: 2008-03-10
Hello,

My problem is the following:

I have a loop that repeats a couple of instruction to generate a sound. I want to be able to stop this loop at one point. To be able to do that, I made a Global Variable (bool Stop). And each time i redo my loop i check it it's true, if it is I quit if not I loop.

In theory it's a good concept except that in practice my application freezes while it's in the loop and i can't seem to press my button that changes Stop (bool var) to true to end my loop.

I know that in VB, the DoEvents function gives you the possibility to do other actions (events ie keypress, mousepress) while in a loop. How do we do that in VC++.

Please provide code or links to ressource pages.

Any suggestions on different approach are welcome

Thanks again,

Luthar
0
Comment
Question by:luthar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6157600
>>How do we do that in VC++.

You'll just have to dipatch the messages and 'drain' the message queue while the loop is running, e.g.

MSG msg;

while ( !bStop)
{

               while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                       {
                           DispatchMessage     (   &msg);
                       }

  // your loop code here
}


Feel free to ask if you need more information!
0
 

Author Comment

by:luthar
ID: 6157637
Thanks, everything works fine now.

Just one question, what's the specs of PeekMessage (in other words, what are the different parameters)?

&msg: obvious...
NULL:
0:
0:
PM_REMOVE:
0
 

Author Comment

by:luthar
ID: 6157642
Thanks, everything works fine now.

Just one question, what's the specs of PeekMessage (in other words, what are the different parameters)?

&msg: obvious...
NULL:
0:
0:
PM_REMOVE:
0
 
LVL 86

Expert Comment

by:jkr
ID: 6157778
Well, here's what the docs say (they're usually smarter than I am :o)

BOOL PeekMessage(
  LPMSG lpMsg,         // pointer to structure for message
  HWND hWnd,           // handle to window
  UINT wMsgFilterMin,  // first message
  UINT wMsgFilterMax,  // last message
  UINT wRemoveMsg      // removal flags
);
 
Parameters
lpMsg
Pointer to an MSG structure that receives message information.

hWnd
Handle to the window whose messages are to be examined.

(REMARK: In our case, if nothing is specified, it's the main message queue)
 
wMsgFilterMin
Specifies the value of the first message in the range of messages to be examined.

wMsgFilterMax
Specifies the value of the last message in the range of messages to be examined.

(REMARK: In our case, if nothing is specified, there is no filtering - but you could focus on e.g. mouse or keyboard messages)

wRemoveMsg
Specifies how messages are handled. This parameter can be one of the following values:
Value Meaning
PM_NOREMOVE Messages are not removed from the queue after processing by PeekMessage.
PM_REMOVE Messages are removed from the queue after processing by PeekMessage.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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