我的程序在单独的线程上执行“繁重的”任务(搜索文件并查找签名)。每个任务都使用PostMessage来调用将记录添加到ListView的过程。
问题是,当ListView(通常)显示许多记录时,我的窗口将冻结。看来我的程序在尖叫要求Application.ProcessMessages。仅进度栏仍在移动。操作完成后,一切恢复正常。
如果删除添加记录的操作,则程序的运行将顺利进行。
你有什么主意吗?
最佳答案
我们曾经达到了可以将多少条消息发布到消息队列的限制。您极有可能也达到了此限制。
从MSDN
There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources.
从The Best Synchronization is No Synchronization
The problem with PostMessage to the main GUI thread is that you can "saturate" the main GUI message pump, and interfere with WM_TIMER and WM_PAINT messages (these messages are only dispatched if the message queue is empty, and PostMessage means that the queue might never be empty. In addition, there is a nominal limit of 10,000 messages in the input queue; after that, PostMessage will discard the message and return FALSE. In addition, even if the 10,000 limit is not hit, even a few hundred messages can mean that the user sees poor response to keyboard and mouse input, an unacceptable situation.
关于multithreading - 如果ListView中有许多记录是从线程中添加的,则窗口会卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5322302/