如何SendMessage()到另一个线程创建的窗口? [英] How do I SendMessage() to a window created on another thread?

查看:462
本文介绍了如何SendMessage()到另一个线程创建的窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我想要 SendMessage 的另一个线程创建的窗口 SendMessage c>。

I have a situation where I want to SendMessage to a window that was created on another thread than the one that is calling SendMessage.

默认行为似乎阻止永远并且无效。

The default behaviour seems to be block forever and not work.

因此我将通话更改为 PostMessage ,它没有阻止发送线程,但是消息不会到达预期的窗口。

So I changed the call to PostMessage, which didn't block the sending thread, but the message never appears to arrive at the intended window.

那么我如何 SendMessage 到一个单独的线程创建的窗口,或者这是不可能的?

So how do I SendMessage to a window created on a separate thread, or is this impossible?

推荐答案

拥有目标窗口的线程是什么?它需要泵送消息,以便能够接收发送或发布到它的消息。通常这是通过一个GetMessage / TranslateMessage / DispatchMessage循环。如果线程正在做其他事情 - 例如阻塞等待事件或互斥或IO完成,或者正在执行一个计算的其他循环中,它不会接收消息:SendMessage到线程将阻塞,PostMessage

What is the thread that owns the target window doing? It needs to be pumping messages in order to be able to receive messages that are either sent or posted to it. Typically this is done by having a GetMessage/TranslateMessage/DispatchMessage loop. If the thread is doing something else - such as blocking waiting for an event or mutex or IO to complete, or is busy in some other loop carrying out a calculation, it won't receive messages: SendMessage to the thread will block, and PostMessage will post but not get delivered.

如果目标线程需要同时管理事件或类似事件,并且它还拥有一个窗口,则可能需要在该线程中使用MsgWaitForMultipleObjects消息循环。

If that target thread needs to both manage events or similar, and it also owns a window, you may need to use MsgWaitForMultipleObjects in that thread's message loop.

这篇关于如何SendMessage()到另一个线程创建的窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆