简体   繁体   English

将消息发送到不存在的窗口可以吗?

[英]Is sending message to a non-existent window Ok?

I have a thread that send update messages to a window, I use ::SendMessage() and ::PostMessage() APIs. 我有一个线程将更新消息发送到窗口,我使用::SendMessage()::PostMessage() API。

I go in and out of multiple dialogs and register the dialog that I am currently in with the thread via the window handle ( m_hWnd ). 我进入和退出多个对话框,并通过窗口句柄( m_hWnd )将我当前所在的对话框注册到线程中。 If I exit all the way out, the main application window doesn't handle these messages. 如果我完全退出,则主应用程序窗口将无法处理这些消息。 For that reason I don't register that window. 因此,我没有注册该窗口。 At this point the thread will have the handle of an older window which now doesn't exist. 此时,线程将具有一个较旧窗口的句柄,该窗口现在不存在。

Is it okay if it sends messages to that non-existent window? 如果它可以向不存在的窗口发送消息,可以吗? I am assuming it should not do any harm but wanted to double check. 我认为它应该不会造成任何伤害,但想再次检查。

No, it is not ok to post a message to a deleted window. 不,将消息发布到已删除的窗口是不行的。
There is no guarantee noone will set up shop at that address just after the previous tennant is gone. 不能保证没有人会在前一个租户离开后立即在该地址开设商店。

If you use a NULL window handle, you'll post a thread message to the current threads message queue. 如果使用NULL窗口句柄,则会将线程消息发布到当前线程消息队列中。 SendMessage as far as I could google shoul be a no-op. 就Google而言, SendMessage应该是无人操作的。
Might be harmless enough. 可能足够无害。

Now, we get tricky: 现在,我们变得棘手:

Under specific cicumstances it does not matter, pre-supposing well-behaved applications. 在特定情况下,这并不重要,只要预先假设行为良好的应用程序即可。

  1. A message like WM_NULL should not make anything happen. WM_NULL这样的消息不应使任何事情发生。
  2. A window-message you globally registered in your application using RegisterWindowMessage , if you can guarantee none of your applications windows created in the meantime will choke on it. 如果可以保证在此期间创建的所有应用程序都不会阻塞窗口,则可以使用RegisterWindowMessage在应用程序中全局注册的窗口消息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM