多线程通信问题!急!

cocia 2001-08-19 09:03:56
我定义了一个父线程,父线称包含5个字线称。
在创建子线程的时候,
我将父线程指针作为子线程的成员变量传进去:
m_pChild->m_pParent=this;
子线程向父线程通信的时候,用消息:

m_pParent->PostThreadMessage(WM_MYMESSAGE,0,0);
在父线程里映射消息处理ON_THREAD_MESSAGE
父线程怎么收不到消息?
调试时,调试窗口有提示:
.........Access Vidation in MFC42D.DLL.....
假如不是子线程,而不别的对象,
m_pParent->PostThreadMessage(WM_MYMESSAGE,0,0);
能成功发送!
这是怎么回事,是线程有什么限制?

...全文
3921 80 打赏 收藏 转发到动态 举报
写回复
用AI写文章
80 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoubo1234 2001-09-14
  • 打赏
  • 举报
回复
gz
hem 2001-09-13
  • 打赏
  • 举报
回复
这么多分的贴子,分不要白不要,只是我不怎么处理类似的问题。
redoak2000 2001-09-10
  • 打赏
  • 举报
回复
*进程是线程的容器,线程是CPU时间片的调度单元。WND 从属于线程。
*每一个线程有一个消息队列,HWND的消息是从相对应的线程的消息队列中取得的。
*消息可以发给HWND(PostMessage),也可以发给THREAD(PostThreadMessage).
*如果使用由CWinThread派生的线程的话,发给THREAD消息用 ON_THREAD_MESSAGE.
*小心使用SendMessage.
*要区分Process , Thread 的HANDLE及ID。Wait时用HANDLE,PostThreadMessage时用ID。
*学会用Sleep(0)。
*UI线程各Worker线程本质是一样的。可以在线程的一个或多个点加入消息处理:PeekMessage or GetMessage.
*要注意线程之间数据的隔离及线程同步。

多了解一下Win32操作系统的消息处理模式。

ZhangSJ 2001-09-05
  • 打赏
  • 举报
回复
收益非浅
baili 2001-09-05
  • 打赏
  • 举报
回复
我刚刚在我的程序中试验成功用::GetCurrentThreadID()和::PostThreadMessage()配合使用,在线程中传递消息,我也摸索过好多种方法,这样做是能成功的,不过可能是我的程序别的地方写的不太好,在频繁的发送线程消息的时候,刚才出现过一次死锁,最后还是用任务管理器解决的问题。注意,应当使用的是win32中的::PostThreadMessage()函数,而不是MFC中的。
羊叔不乖 2001-09-04
  • 打赏
  • 举报
回复
水清无鱼 2001-09-04
  • 打赏
  • 举报
回复
关注!!!
nilm 2001-09-04
  • 打赏
  • 举报
回复
PostThreadMessage

The function fails if the specified thread does not have a message queue. The system creates a thread's message queue when the thread makes its first call to one of the Win32 USER or GDI functions. For more information, see the Remarks section.

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:

1.Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
2.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(&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.

sonicsky 2001-09-04
  • 打赏
  • 举报
回复
up
eetocs 2001-09-04
  • 打赏
  • 举报
回复
以下是msdn的postthreadmessage的文档中的一部分:
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails.

请注意:The thread to which the message is posted must have created a message queue

根据我的记忆,如果thread没有window,就不会有message queue与之关联。请指正
jack_fu 2001-09-04
  • 打赏
  • 举报
回复
以下是msdn关于postthreadmessage api的文档中的一部分:

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:

请注意:The thread to which the message is posted must have created a message queue

根据我的记忆,如果没有window,是不会有message queue与线程相关的。请指正
jack_fu 2001-09-04
  • 打赏
  • 举报
回复
晕,请看看advanced windows programming,那里有关message讲的很清楚,如果没有调用createwindows来创建windows的话,是没有消息结构与你的线程关联的。就算调用peekmessage也拿不到消息。

原文我记不清了,等我回去查查再来。欢迎讨论。

davidlxm(davidlxm):关于你讲的postthreadmessage的例程在msdn的哪里?不是cwintread的吧?
jack_fu 2001-09-04
  • 打赏
  • 举报
回复
晕,请看看adviced windows programming,那里有关message讲的很清楚,如果没有调用createwindows来创建windows的话,是没有消息结构与你的线程关联的。就算调用peekmessage也拿不到消息。

原文我记不清了,等我回去查查再来。欢迎讨论。

davidlxm(davidlxm):关于你讲的postthreadmessage的例程在msdn的哪里?不是cwintread的吧?
cocia 2001-09-02
  • 打赏
  • 举报
回复
就这个机会,
我想再发几个关于多线程问题的贴子,
把多线程彻底弄明白!
还请大家关注!!!
fengzhao 2001-09-02
  • 打赏
  • 举报
回复
关于多任务的实现,有机会我会说一说的。
fengzhao 2001-09-02
  • 打赏
  • 举报
回复
实际关于进程的通信只有两种方法,一种是使用内存映射文件,另一种是使用环境变量。再有,在父进程中用CreateProcess创建子进程是要使bInheritHandles为TRUE还有传给子进程的父进程指针是在进程地址中的一个映射地址,所以不管用的,还有关于ThreadID的问题也是如此,如果你使用了GetCurrentThreadId()得到的也只是映射id,要传给其他进程是要调用DuplicateHandle来得到在此进程中,此线程的实际指针。
nilm 2001-09-02
  • 打赏
  • 举报
回复
线程间通讯的方式很多,PostThreadMessage肯定是可以的,看一下文档,直接用API。
不行就创建一个窗口嘛,条条道路通罗马。
actinia 2001-09-01
  • 打赏
  • 举报
回复
up!
cocia 2001-09-01
  • 打赏
  • 举报
回复
看来我只有听的份了!
不过多线程的调试我只能凭感觉了!:(
sorry2000 2001-08-31
  • 打赏
  • 举报
回复
谁说PostThreadMessage不能用在98?!#·¥#·%¥
很多不懂的人在这瞎说!
加载更多回复(60)

15,471

社区成员

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

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