windows SendMessage(HWND_BROADCAST, ....) 挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:37:33  来源:igfitidea点击:

SendMessage(HWND_BROADCAST, ....) hangs

windowswinapi

提问by AH.

When I use theSendMessage function with HWND_BROADCAST, the application hangs up. There is no response from the application for long time.

当我使用theSendMessage 函数时HWND_BROADCAST,应用程序挂断。很长时间没有收到申请的回复。

Can anyone explain why?

谁能解释为什么?

回答by John Knoeller

This will happen when there is an process that has a top level window, but isn't calling GetMessage or PeekMessage on the thread that created the window.

当有一个进程具有顶级窗口,但没有在创建窗口的线程上调用 GetMessage 或 PeekMessage 时,就会发生这种情况。

For backward compatibility with Windows 3.0, SendMessage will not return until all of the top level windows in the system have responded to your broadcast. This behavior made sense back before Windows was multithreaded, because SendMessage(), even when sending to other processes would never block.

为了与 Windows 3.0 向后兼容,在系统中的所有顶级窗口都响应您的广播之前,SendMessage 不会返回。在 Windows 多线程之前,这种行为是有意义的,因为 SendMessage(),即使发送到其他进程也永远不会阻塞。

But beginning with Win32, when you SendMessage to a window in another process, what actually happens is your thread blocks until the thread in the other process wakes up and handles the message. If that thread is busy, or just not pumping messages, then you wait forever.

但是从 Win32 开始,当您将消息发送到另一个进程中的窗口时,实际发生的是您的线程阻塞,直到另一个进程中的线程唤醒并处理该消息。如果该线程很忙,或者只是没有发送消息,那么您将永远等待。

For that reason you should always use SendNotifyMessageor SendMessageTimeoutwhen you use HWND_BROADCAST, or are otherwise sending messages to windows owned by other processes.

出于这个原因,您应该始终使用SendNotifyMessageSendMessageTimeout当您使用 HWND_BROADCAST 时,或者以其他方式向其他进程拥有的窗口发送消息。

回答by Dumb Guy

This is because when SendMessageis called with HWND_BROADCAST, it first enumerates all the windows available and then calls SendMessagefor each of those windows. SendMessagewill not return until the window has finished processing the message. If a single window is taking a long time to process the message, the entire call will be delayed.

这是因为当用SendMessage调用时HWND_BROADCAST,它首先枚举所有可用的窗口,然后调用SendMessage这些窗口中的每一个。SendMessage在窗口完成处理消息之前不会返回。如果单个窗口需要很长时间来处理消息,则整个调用将被延迟。

回答by Stabledog

There is a SendMessageTimeout which will limit the amount of time your application blocks while waiting for the receiver to accept.

有一个 SendMessageTimeout 将限制您的应用程序在等待接收者接受时阻塞的时间。

Another workaround is to launch multiple threads and have them deliver multiple messages at once (i.e. to it in parallel). Then if one of the receivers is hung, you don't kill your entire app.

另一种解决方法是启动多个线程并让它们一次传递多条消息(即并行发送)。然后,如果其中一个接收器挂了,您就不会杀死整个应用程序。

回答by Bob Moore

There is at least one process out there that has a message pump but isn't pumping messages. SendMessage doesn't return until all receivers have processed the message... so it doesn't return. You can try using SendMessageTimeout instead to get around this.

至少有一个进程具有消息泵但不泵送消息。在所有接收者都处理完消息之前,SendMessage 不会返回……所以它不会返回。您可以尝试使用 SendMessageTimeout 来解决此问题。

Incidentally, this is why launching a process and waiting on its process handle can be fraught with problems. I describe this on my website here.

顺便说一句,这就是为什么启动一个进程并等待它的进程句柄会充满问题的原因。我在我的网站上描述了这一点