等待AfxBeginThread/CWinThread消息泵处于活动状态? [英] Wait for AfxBeginThread/CWinThread message pump to be active?

查看:204
本文介绍了等待AfxBeginThread/CWinThread消息泵处于活动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用AfxBeginThread并使用CWinThread在MFC应用程序中启动UI线程.

I'm calling AfxBeginThread and using CWinThread to spin up a UI thread in my MFC app.

我注意到,如果我的主线程在CWinThread :: InitInstance()函数返回之前尝试将PostThreadMessage()移至新线程,则PostThreadMessage()将返回错误:无效的线程句柄.

I've noticed that if my main thread tries to PostThreadMessage() to my new thread before the CWinThread::InitInstance() function returns, then PostThreadMessage() will return the error: invalid thread handle.

我的猜测是,直到InitInstance返回之后,才会在新线程上设置消息泵.我在AfxBeginThread中看到的示例代码和我阅读的文档并不能很好地解释这种行为,或者显示了一种模式来等待线程被初始化.

My guess is that the message pump on the new thread isn't setup until after InitInstance returns. The example code I've seen for AfxBeginThread and the documentation I've read don't do a good job of explaining this behavior, or show a pattern to wait for the thread to be initialized.

在InitInstance返回并且线程的消息泵准备好接收消息之前,阻塞主线程的最佳方法是什么?

What's the best way of blocking my main thread until InitInstance has returned and the thread's message pump is ready to receive messages?

推荐答案

Peter的回答很好,因为他认识到您只需要等待消息队列的创建"即可.该发现导致以下链接显示在相关答案中: WaitForSingleObject返回由于句柄无效而导致等待失败,这说明了执行彼得建议的更简单方法.

Peter's answer is good in that he recognized that "you just need to wait for the message queue to be created". That revelation caused the following link to show up in the related answers: WaitForSingleObject returns wait failed due to invalid handle, which demonstrates an easier way to do what Peter suggests.

这篇关于等待AfxBeginThread/CWinThread消息泵处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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