简体   繁体   English

来自目标线程的PostThreadMessage

[英]PostThreadMessage from target thread

Today, I've seen a code like: 今天,我看到了如下代码:

void Foo()
{
    MyMsgStruct myMsg;

    /* omission for simplicity */

    /* send to update thread */
    PostThreadMessage(myThreadId, myMessage, (WPARAM)myMsg, NULL);
}

What happens when Foo() is called from the same thread as myThreadId , ie from the same thread that's supposed to be the target thread ? 当从与myThreadId相同的线程(即从应该作为目标线程的同一线程Foo()调用Foo()时会发生什么? Is it a performance issue or just bad written code or both? 是性能问题,还是写得不好的代码,还是两者兼而有之?

I believe it may affect performance since I believe it would queue the message to the thread's queue instead of just doing what it's supposed to do, therefore slowing the program a little. 我认为这可能会影响性能,因为我认为它将消息排队到线程的队列中,而不仅仅是执行应做的事情,因此会使程序变慢一点。

Thread safety is not my concern for this question . 线程安全不是我关心的这个问题

Nothing very special, it just takes a while before whatever code is supposed to run will be called. 没什么特别的,只需要一段时间就可以调用应该运行的任何代码。 Just a delay, it doesn't necessarily make your program slower. 只是一个延迟,并不一定会使您的程序变慢。 Sometimes you do it on purpose, like wanting to respond to a Windows message but doing it right away causing re-entrancy issues. 有时您是故意这样做的,例如希望响应Windows消息,但立即这样做会导致重新进入问题。

Using PostThreadMessage should however almost always be avoided. 但是,几乎应始终避免使用PostThreadMessage。 Really Bad Things happen when the thread also creates windows, almost always the case since you tend to post to the UI thread to get code to, say, update a window. 当线程还创建窗口时,确实会发生不好的事情,几乎总是这样,因为您倾向于发布到UI线程来获取代码,例如更新窗口。 Messages fall in the bit bucket whenever a modal loop is entered. 每当进入模态循环时,消息就会落入位桶中。 Like the one that is used to resize a window. 类似于用于调整窗口大小的窗口。 Or displays MessageBox. 或显示MessageBox。 Always favor posting to a window instead, the message cannot get lost. 始终偏向于发布到窗口,消息不会丢失。 Do check the return value of PostMessage(). 请检查PostMessage()的返回值。

Creating a dummy invisible window whose window procedure handles these messages is generally a good idea. 创建一个虚拟的不可见窗口,其窗口过程处理这些消息通常是一个好主意。 You now also have a way to check if you need to post or can execute directly with SendMessage. 现在,您还可以检查是否需要发布或可以直接使用SendMessage执行。 Compare GetWindowThreadProcessId with GetCurrentThreadId. 比较GetWindowThreadProcessId与GetCurrentThreadId。

The message goes into the message queue of the target thread ID, regardless of which thread is calling PostThreadMessage() . 消息进入目标线程ID的消息队列,而不管哪个线程正在调用PostThreadMessage() It is perfectly valid for a thread to post messages to itself. 线程向自身发布消息是完全有效的。 The message will be processed when the thread pumps its message queue for new messages. 当线程为新消息泵送其消息队列时,将处理该消息。 Sometimes a thread really may want to have a delayed action, so a posted message is good for that. 有时,一个线程确实可能想要延迟动作,因此发布消息对此很有用。 If a thread wants to do something immediately, there is no point in calling PostThreadMessage() , just perform the operation directly. 如果线程想要立即执行某操作,则调用PostThreadMessage()没有任何意义,只需直接执行该操作即可。 There is no equivalent to SendMessage() for thread messages. 没有等同于SendMessage()的线程消息。

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

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