PostThreadmessage为什么不能向CWinThread对象发消息怎么回事

zhangjustice 2003-06-27 05:55:58
我已经建立了一个CWinThread对象,用ON_THREAD_MESSAGE定义
消息映射。也知道了它的线程ID,但为什么用windows的
PostTreadMessage根据线程ID发消息时,这个对象没有反应。
解决的办法是什么呢,请各位高手指点迷津。
...全文
429 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
penter 2003-07-17
  • 打赏
  • 举报
回复
RESOLUTION
The following code demonstrates how to call PostThreadMessage() in the InitInstance() of the CWinApp-derived class using a single-threaded application. The principle is the same for secondary threads except that the code shown here would be put in your alternate CWinThread-derived class.

Visual C++ 4.2 does support handling messages posted to threads. For more information, see ON_THREAD_MESSAGE in the Visual C++ 4.2 documentation.

NOTE: MFC worker threads do not have a message loop/pump associated with them so you must use a user-interface thread.
Sample Code
/* Compile options needed:

standard MFC project generated by AppWizard */

BOOL CThreadMsgApp::PreTranslateMessage(MSG* pMsg)
{
// Is it the Message you want?
// You can use a switch statement but because this is
// only looking for one message, you can use the if/else
if (pMsg->message == WM_USER+2268)
{
// Call the function to handle this message
OnReceivedCommand(pMsg->wParam,pMsg->lParam);
// Tell MFC no more processing is needed
return TRUE;
}
else
// Call MFC to have it continue processing the message
return CWinThread::PreTranslateMessage(pMsg);
}

BOOL CThreadMsgApp::InitInstance()
{
WPARAM wParam;
LPARAM lParam;
wParam = MAKEWPARAM(0,0); // We can put whatever we
lParam = MAKELPARAM(0,0); // want in wParam & lParam

// Send the user-defined Thread Message
// m_nThreadID is a member of CWinThread that holds the thread ID
PostThreadMessage(m_nThreadID, WM_USER+2268, wParam, lParam);

return TRUE;
}

void CThreadMsgApp::OnReceivedCommand(WPARAM wParam, LPARAM lParam)
{
// You can do whatever you want in here, this is simply
// sending output to the debug window
TRACE0("Received WM_USER+2268!!\n");
}

初学,不知道,请指教;
penter 2003-07-17
  • 打赏
  • 举报
回复
当一个线程第一次被建立时,系统假定线程不会陪被用于任何与用户相关的任务。以此来减少线程对系统资源的要求。但当线程调用与用户界面相关的函数的时候(例如检查它的消息队列或建立一个窗口),系统就会为该线程分配其他资源,其中就有THREADINFO结构,它是窗口消息的基础;

但CWinThread有用户界面相关的东东吗?也就是,它是否有自己的窗口吗?

When you call PostThreadMessage(), the message is placed in the thread's message queue. However, because messages posted this way are not associated with a window, MFC will not dispatch them to message or command handlers. In order to handle these messages, override the PreTranslateMessage() function of your CWinApp-derived class, and handle the messages manually.

看来没有;MFC不转发与窗口无关的线程消息;



zhangjustice 2003-07-12
  • 打赏
  • 举报
回复
大哥们,我已经重载了PreTranslateMessage 函数,但还是不行,不仅重载了CWinThread的
而且重载了App的,也不行,发送成功的消息不知道到哪去了,这到底是怎么回事?
zhangjustice 2003-07-12
  • 打赏
  • 举报
回复
请问问PreTranslateMessage的怎样重载呀?
penter 2003-07-02
  • 打赏
  • 举报
回复
mark
;
yongdu 2003-07-02
  • 打赏
  • 举报
回复
你如果发送成功,确信是没收到消息的话,你有可能要在你的线程类内重载PreTranslateMessage() , 如果你没有关联窗口的话。
littlebiy 2003-06-29
  • 打赏
  • 举报
回复
如果接收消息处理的函数执行时间太长,而同一消息又特别多的话,很容易造成消息的阻塞。
因此如果是消息函数执行时间太长的,一般不必考虑消息的形式,可以考虑在发消息前将发送的数据存储到一个链表中,然后建立一个线程,在线程中再慢慢的处理这些链表中的数据。
另外,也可以在接收消息的函数中,立即处理消息,免得引发一步的阻塞。
ahao 2003-06-29
  • 打赏
  • 举报
回复
另一个帖子里回答你了
JetLuo 2003-06-29
  • 打赏
  • 举报
回复
自己写消息循环:
int CMyThread::Run()
{
MSG msg;
while(1)
{
MsgWaitForMultipleObjects( 0,
NULL,
FALSE,
INFINITE,
QS_ALLINPUT );
while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
switch( msg.message )
{
case WM_MESS:
DoSomething()
break;

default:

break;

}
}
}

return CWinThread::Run();
}
zhangjustice 2003-06-29
  • 打赏
  • 举报
回复
楼上的哥们的建议我试了,不过用PostThreadMessage通过线程发的消息还是接收不到
这是怎么回事?
zhangjustice 2003-06-27
  • 打赏
  • 举报
回复
用CWinThread类中的PostThreadMessage就能收到,msdn是这样写的
The posted message is mapped to the proper message handler by the message map macro ON_THREAD_MESSAGE.

Note When calling the Windows PostThreadMessage function within an MFC application, the MFC message handlers are not called. For more information, see the Knowledge Base article, "PRB: MFC Message Handler Not Called with PostThreadMessage()" (Q142415).
这到底是怎么回事,请各位高手指点,如果我要是重载run函数得怎么办?
yongdu 2003-06-27
  • 打赏
  • 举报
回复
应该用while(!PostThreadMessage(...)) Sleep(10)

可能用一次PostThreadMessage不成功.

ahalf 2003-06-27
  • 打赏
  • 举报
回复
没有消息队列

15,471

社区成员

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

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