Modal operation using IMessageFilter and DoEvents

This is a Windows Forms application. I have a function that captures some mouse events modally until a condition is met. For example, I would like to wait for the user to select a point in the client area of ​​the window (or perhaps cancel the operation with 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 pretty well, except it uses almost 100% of the 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 correct way to use IMessageFilter

and DoEvents

? How can I transfer control to the OS before receiving the message? Any equivalent GetMessage

in a managed world?

+1


source to share


1 answer


You can sleep the thread for 500ms between calls DoEvents()

. Experiment with different values ​​to figure out what is right.



+1


source







All Articles