Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - SendMessage(HWND_BROADCAST, ....) hangs

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

Can anyone explain why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...