简体   繁体   English

处理消息太慢,导致生涩,无响应的用户界面 - 如何使用多个线程来缓解这种情况?

[英]Processing messages is too slow, resulting in a jerky, unresponsive UI - how can I use multiple threads to alleviate this?

I'm having trouble keeping my app responsive to user actions. 我无法让我的应用响应用户操作。 Therefore, I'd like to split message processing between multiple threads. 因此,我想在多个线程之间拆分消息处理。

Can I simply create several threads, reading from the same message queue in all of them, and letting which ever one is able process each message? 我可以简单地创建多个线程,从所有线程中读取相同的消息队列,并让哪个线程能够处理每个消息?

If so, how can this be accomplished? 如果是这样,怎么办呢?

If not, can you suggest another way of resolving this problem? 如果没有,你能提出另一种解决这个问题的方法吗?

You cannot have more than one thread which interacts with the message pump or any UI elements. 您不能有多个与消息泵或任何UI元素交互的线程。 That way lies madness. 那种方式就是疯狂。

If there are long processing tasks which can be farmed out to worker threads, you can do it that way, but you'll have to use another thread-safe queue to manage them. 如果有很长的处理任务可以用于工作线程,那么你可以这样做,但是你必须使用另一个线程安全的队列来管理它们。

If this were later in the future, I would say use the Asynchronous Agents APIs (plug for what I'm working on) in the yet to be released Visual Studio 2010 however what I would say given todays tools is to separate the work, specifically in your message passing pump you want to do as little work as possible to identify the message and pass it along to another thread which will process the work (hopefully there isn't Thread Local information that is needed). 如果这是将来的后期,我会说在尚未发布的Visual Studio 2010中使用异步代理API(插件用于我正在处理的内容)但是我今天所说的工具是分开工作,特别是在您的消息传递泵中,您希望尽可能少地工作以识别消息并将其传递给另一个将处理工作的线程(希望不存在所需的线程本地信息)。 Passing it along to another thread means inserting it into a thread safe queue of some sort either locked or lock-free and then setting an event that other threads can watch to pull items from the queue (or just pull them directly). 将它传递给另一个线程意味着将其插入到某种线程安全队列中,无论是锁定还是无锁,然后设置其他线程可以监视的事件以从队列中提取项目(或者直接拉动它们)。 You can look at using a 'work stealing queue' with a thread pool for efficiency. 您可以使用带有线程池的“工作窃取队列”来提高效率。

This will accomplish getting the work off the UI thread, to have the UI thread do additional work (like painting the results of that work) you need to generate a windows message to wake up the UI thread and check for the results, an easy way to do this is to have another 'work ready' queue of work objects to execute on the UI thread. 这将完成UI线程的工作,让UI线程做额外的工作(比如绘制工作的结果),你需要生成一个窗口消息来唤醒UI线程并检查结果,一个简单的方法要做到这一点,就是要在UI线程上执行另一个“工作就绪”工作对象队列。 imagine an queue that looks like this: threadsafe_queue<function<void(void)> basically you can check if it to see if it is non-empty on the UI thread, and if there are work items then you can execute them inline. 想象一个看起来像这样的队列: threadsafe_queue<function<void(void)>基本上你可以检查它是否在UI线程上看是否为非空,如果有工作项,那么你可以内联执行它们。 You'll want the work objects to be as short lived as possible and preferably not do any blocking at all. 你会希望工作对象尽可能短暂,最好不要做任何阻塞。

Another technique that can help if you are still seeing jerky movement responsiveness is to either ensure that you're thread callback isn't executing longer that 16ms and that you aren't taking any locks or doing any sort of I/O on the UI thread. 另一种技术可以帮助你,如果你仍然看到不稳定的运动响应性,要么确保你的线程回调没有执行16ms的更长时间,并且你没有在UI上进行任何锁定或进行任何类型的I / O线。 There's a series of tools that can help identify these operations, the most freely available is the 'windows performance toolkit' . 有一系列工具可以帮助识别这些操作,最免费的是“Windows性能工具包”

在处理长操作时创建单独的线程,即保持简单,问题在于您运行的某些代码花费的时间太长,这是应该有一个单独线程的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 std::unordered_map 很慢,我可以更有效地使用它来缓解这种情况吗? - Why is std::unordered_map slow, and can I use it more effectively to alleviate that? 如何创建多线程清洁器? - How can i create multiple threads cleaner? 如何在图像处理中有效使用线程? - How to Use threads efficiently in Image Processing? 用于向量处理的多线程 - multiple threads for vector processing 如何在COM中跨多个线程使用LoadLibrary? - How Do I use LoadLibrary in COM Accross Multiple Threads? 如何将矩阵提升为具有多个线程的幂? - How can I raise a matrix to a power with multiple threads? 我可以在MFC中有多个GUI线程吗? - Can I have multiple GUI threads in MFC? 如何使用线程实例化多个QApplication - How to use threads to instantiating multiple QApplication 代码使用开关 function 分别处理消息,如何使用来自 2 条消息的数据? - Code uses a switch function to process messages separately, how can I use data from 2 messages? 如何在没有同步的情况下使用多个线程(2、4、8、16 个线程)在循环(10,100、1000 个循环)中打印字符串? - How can I print a string in a loop (10,100, 1000 cycles) using multiple threads (2, 4,8 , 16 threads) with no synchronization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM