How to program to prevent the Windows “Not Responding” dialog [duplicate]

僤鯓⒐⒋嵵緔 提交于 2021-01-28 01:42:27

问题


When an application fails to be responsive for 5 seconds (source), Windows can display "(Not Responding)" in the title bar and in some cases show a "not responding" dialog:

Ideally, the 5+ second execution should not block the main/event-processing thread, but is there a simple (e.g. 1 liner for MFC C++) way to communicate to Windows that the main thread is busy and shouldn't be treated as a "Not Responding" application to be closed? Is the quickest hack to simply periodically call peak PeekMessage with PM_NOREMOVE?


回答1:


There really are no hacks to solve this. Any monkeying about with the message pump can lead to all manner of disaster, especially with COM and other system message processing.

Don't hold up the main thread.

Move the longer running tasks to a background or worker thread and either poll a future for completion or have the thread post a message back to the GUI to signal it is complete and then retrieve the result required.




回答2:


In Windows 3.1 world all those eons ago, the answer was to divide your work into short-duration chunks, run the chunks in your window's message procedure and go from chunk to chunk by calling PostMessage.

Nowadays you update the window to show that the work is in progress, spawn a thread and call some PostMessage equivalent at the end of the thread so that your window can update itself back and show the results.




回答3:


No, there's no quick hack, because your app is indeed not responding if you use the main thread like that.



来源:https://stackoverflow.com/questions/33135036/how-to-program-to-prevent-the-windows-not-responding-dialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!