首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

c++ thread,该如何解决

2013-07-09 
c++ thread用windows api createthread 函数创建线程,有消息队列? 用什么方法可以使其变成ui线程 有消息队

c++ thread
用windows api createthread 函数创建线程,有消息队列? 用什么方法可以使其变成ui线程 有消息队列???
请大家帮忙解释解释??????? C++ 线程 Windows API 多线程
[解决办法]
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use the following method to handle this situation.
Create an event object, then create the thread.
Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage.
In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue.
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
Set the event, to indicate that the thread is ready to receive posted messages.
[解决办法]
在线程中创建窗口,创建消息循环

DWORD WINAPI ThreadProc(LPVOID lParam)
{
 CDialog* pDlg = new CXXXDlg;
 pDlg->Create(....);
 pDlg->ShowWindow(SW_SHOW);

 MSG msg;
 while(GetMessage(NULL, &msg, 0, 0))
 {
    if(MSG_EXITTHREAD == msg.message)
       break;  // Exit Thread
    TranslateMessage(&msg);
    DispatchMessage(&msg);
 }
 return 0;
}

[解决办法]
如楼上,补充一点:不必创建窗口,下面这句也没必要,仔细看GetMessage帮助就知道了。
if(MSG_EXITTHREAD == msg.message)       break;  // Exit Thread

另,线程都有消息队列,但不一定有消息循环,你要的是消息循环,这是个概念问题。

热点排行