使用 IMessageFilter 和 DoEvents 进行模态操作 [英] Modal operation using IMessageFilter and DoEvents

查看:25
本文介绍了使用 IMessageFilter 和 DoEvents 进行模态操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 Windows 窗体应用程序.我有一个函数可以模态地捕获一些鼠标事件,直到满足条件.例如,我想在函数返回之前等待用户在窗口的客户区中选择一个点(或可选地使用 Escape 键取消操作).我正在使用以下结构:

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);

除了在控制进入 while 循环时占用近 100% 的 CPU 使用率之外,这非常有效.我正在寻找类似于下图所示的替代方案:

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);

使用 IMessageFilterDoEvents 的正确方法是什么?在收到消息之前,如何将控制权交给操作系统?托管世界中的任何 GetMessage 等价物?

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?

推荐答案

您可以在 DoEvents() 调用之间让线程休眠 500 毫秒左右.尝试不同的值,看看什么感觉是正确的.

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

这篇关于使用 IMessageFilter 和 DoEvents 进行模态操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆