Agnel Kurian
Agnel Kurian

Reputation: 59466

Modal operation using IMessageFilter and DoEvents

This is a Windows Forms application. I have a function which captures some mouse events modally till a condition is met. For example, I would like to wait for the user to select a point in the window's client area (or optionally cancel the operation using the Escape key) before the function returns. I am using the following structure:

Application::AddMessageFilter(someFilter);
while(someFilter->HasUserSelectedAPoint_Or_HitEscapeKey()){
    Application::DoEvents();
}
Application::RemoveMessageFilter(someFilter);

This works quite nicely except for taking up nearly 100% CPU usage when control enters the while loop. I am looking for an alternative similar to what is shown below:

Application::AddMessageFilter(someFilter);
while(someFilter->HasUserSelectedAPoint_Or_HitEscapeKey()){
    // Assuming that ManagedGetMessage() below is a blocking
    // call which yields control to the OS
    if(ManagedGetMessage())
        Application::DoEvents();
}
Application::RemoveMessageFilter(someFilter);

What is the right way to use IMessageFilter and DoEvents? How do I surrender control to the OS till a message is received? Any GetMessage equivalent in the managed world?

Upvotes: 1

Views: 717

Answers (1)

lc.
lc.

Reputation: 116498

You could sleep the thread for 500ms or so between DoEvents() calls. Experiment with different values to see what feels right.

Upvotes: 1

Related Questions