工作者线程如何接收消息?

zaodt 2007-09-08 09:17:58
工作者线程如何接收消息?

请高手给我指明个大概方向即可!
...全文
640 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
罗嘉贇 2007-09-11
  • 打赏
  • 举报
回复
实际上只要用winmain()为程序入口,然后就可以用消息循环了。至于窗口你想要就要,不想要就不要。没什么的
sunlin7 2007-09-11
  • 打赏
  • 举报
回复
从MSDN上找到:
PostThreadMessage
...
Remarks
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation:

Call PostThreadMessage. If it fails, call theSleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
Create an event object, then create the thread. Use theWaitForSingleObject 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(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create the message queue. Set the event, to indicate that the thread is ready to receive posted messages.
The thread to which the message is posted retrieves the message by calling the GetMessage or PeekMessage function. The hwnd member of the returned MSG structure is NULL.

就讲到使用:PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) 建立消息队列
casinosun 2007-09-11
  • 打赏
  • 举报
回复
http://www.cppblog.com/sandy/archive/2005/12/31/2320.html
arong1234 2007-09-11
  • 打赏
  • 举报
回复
线程就应该永远在GetMessage处等,直到它收到消息啊?你确信消息确实发给线程了?
WizardK 2007-09-11
  • 打赏
  • 举报
回复
当时是参考WIN32 Application的程序架构来做的
WizardK 2007-09-11
  • 打赏
  • 举报
回复
我只做过用窗口接收的,就是建立好一个窗口用它的消息队列来收消息,至于ouyh12345(五岭散人)说的第二种方法没有实践过,等等试试

LPCTSTR CLASS_NAME = L"CXXX Class";
LPCTSTR WINDOW_TITLE = L"XXXWinTitle";

UINT XXXProc(LPVOID pParam);
HWND CreateMsgWin(CString strClass, CString strTitle);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

/////////////////////////////////////////////////////////////////////////////
// Create Windows for Messages
HWND CreateMsgWin(CString strClass, CString strTitle)
{
HINSTANCE hInstance = NULL;
HWND hWnd = NULL;
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = strClass;
RegisterClass(&wc);

hWnd = CreateWindow(strClass, strTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (hWnd)
{
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
}

return hWnd;
}

/////////////////////////////////////////////////////////////////////////////
// Window Messages Processing
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_XXX:
// Do What You Want to Do
break;
case WM_CREATE:
break;
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}

return 0;
}

/////////////////////////////////////////////////////////////////////////////
// Working thread
UINT XXXProc(LPVOID lpParam)
{
MSG msg;
CXXX * ppParam = (CXXX *)lpParam;

ppParam->m_hWin = CreateMsgWin(CLASS_NAME, WINDOW_TITLE);

// Just Loop Here
while (GetMessage(&msg, ppParam->m_hWin, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}
zaodt 2007-09-11
  • 打赏
  • 举报
回复
我用上边的方法,工作线程接收不到消息。

程序好像等在这个函数处 GetMessage
wltg2001 2007-09-11
  • 打赏
  • 举报
回复
所以:问题不在于到底收到没收到消息,而在于你发的啥消息,WM_QUIT永远收不到,这是系统规定好的
=======================================
看不明白,WM_QUIT被收到时,GetMessage返回假,不就说明收到了吗?
arong1234 2007-09-11
  • 打赏
  • 举报
回复
GetMessage规定如果收到WM_QUIT,就直接返回FALSE。如果你发送的是WM_QUIT,收不到是正常的,因为系统的API不会给你一个消息,而只会返回一个失败

所以:问题不在于到底收到没收到消息,而在于你发的啥消息,WM_QUIT永远收不到,这是系统规定好的
zaodt 2007-09-11
  • 打赏
  • 举报
回复

那有谁真正做过吗?

MFC 下,在工作者线程中接收主线程发来的消息?
wltg2001 2007-09-11
  • 打赏
  • 举报
回复
而主线程发送消息的 PostThreadMessage 函数发送却是成功的
================================
寄送消息成功了只表示消息发送出去了,并不代表线程一定会收到消息并作相应处理
zaodt 2007-09-11
  • 打赏
  • 举报
回复

因为 工作线程 接收到 WM_QUIT 消息后,GetMessage 函数返回假,所以 工作线程 会退出,并显示消息“WM_QUIT”。

可是,主线程向工作线程发送 WM_QUIT 消息后,工作线程却没有任何反应。

而主线程发送消息的 PostThreadMessage 函数发送却是成功的。
zaodt 2007-09-11
  • 打赏
  • 举报
回复

事实上,以上资料我都参考过了,可还是不行,PostThreadMessage 函数我也判读了返回值,的确发送了,而且我发送的是 WM_QUIT 消息,可是工作线程中仍然无法接收到消息。

下面是我的线程函数体:

//////////////////////////////////////////////////////////////

MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_REMOVE);

BOOL bRet;

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
AfxMessageBox("-1");
return 0;
}
else
{
//TranslateMessage(&msg);
//DispatchMessage(&msg);

switch(msg.message)
{
case MY_MSG:
AfxMessageBox("It's running.");
break;
default:
AfxMessageBox("default.");
break;
}
}
}

AfxMessageBox("WM_QUIT");

return 0;

//////////////////////////////////////////////////////////////

可是,我即使发送 WM_QUIT 消息,工作线程也没有反应。

请高手指点!
templarzq 2007-09-10
  • 打赏
  • 举报
回复
dword winapi xxx()
{
...
while(1)
{
peekmessage(message,....);
switch(message)
{
...
}
}
return 0;
}
ouyh12345 2007-09-10
  • 打赏
  • 举报
回复
可以。
一种方法是创建隐藏的窗口。
另一种方法是创建消息循环,然后其它线程用PostThreadMessage给它发消息
cayido 2007-09-10
  • 打赏
  • 举报
回复
while( GetMessage(....))
{

}
关键是自己写个消息环
具体新建个WIN32的样板程序看 建议还是继承WINTHREAD比较好
cayido 2007-09-10
  • 打赏
  • 举报
回复
可以
和WIN32一样建立 ()
arong1234 2007-09-10
  • 打赏
  • 举报
回复
就应该是cayido的方式,处理消息循环
zaodt 2007-09-09
  • 打赏
  • 举报
回复
Up
zaodt 2007-09-08
  • 打赏
  • 举报
回复

那请问,我可不可以在 工作者 线程中建立消息队列?
加载更多回复(1)

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧