工作者线程如何接收消息?
工作者线程如何接收消息?
请高手给我指明个大概方向即可!
[解决办法]
只有UI线程(有消息队列的)才能接收消息。
[解决办法]
可以
和WIN32一样建立 ()
[解决办法]
while( GetMessage(....))
{
}
关键是自己写个消息环
具体新建个WIN32的样板程序看 建议还是继承WINTHREAD比较好
[解决办法]
可以。
一种方法是创建隐藏的窗口。
另一种方法是创建消息循环,然后其它线程用PostThreadMessage给它发消息
[解决办法]
dword winapi xxx()
{
...
while(1)
{
peekmessage(message,....);
switch(message)
{
...
}
}
return 0;
}
[解决办法]
就应该是cayido的方式,处理消息循环
[解决办法]
线程就应该永远在GetMessage处等,直到它收到消息啊?你确信消息确实发给线程了?
[解决办法]
http://www.cppblog.com/sandy/archive/2005/12/31/2320.html
[解决办法]
从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) 建立消息队列
[解决办法]
实际上只要用winmain()为程序入口,然后就可以用消息循环了。至于窗口你想要就要,不想要就不要。没什么的
[解决办法]
而主线程发送消息的 PostThreadMessage 函数发送却是成功的
================================
寄送消息成功了只表示消息发送出去了,并不代表线程一定会收到消息并作相应处理
[解决办法]
GetMessage规定如果收到WM_QUIT,就直接返回FALSE。如果你发送的是WM_QUIT,收不到是正常的,因为系统的API不会给你一个消息,而只会返回一个失败
所以:问题不在于到底收到没收到消息,而在于你发的啥消息,WM_QUIT永远收不到,这是系统规定好的
[解决办法]
所以:问题不在于到底收到没收到消息,而在于你发的啥消息,WM_QUIT永远收不到,这是系统规定好的
=======================================
看不明白,WM_QUIT被收到时,GetMessage返回假,不就说明收到了吗?
[解决办法]
我只做过用窗口接收的,就是建立好一个窗口用它的消息队列来收消息,至于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;
}
[解决办法]
当时是参考WIN32 Application的程序架构来做的