Solved

DoEvents in VC++/MFC

Posted on 2001-06-05
4
796 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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