在MFC多线程编程中,主线程和Seondary-Thread之间的链接如何发生? [英] How the link happen in between Main-Thread and Seondary-Thread in MFC multithreading programming?

查看:97
本文介绍了在MFC多线程编程中,主线程和Seondary-Thread之间的链接如何发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


任何人都可以通过以下方式澄清疑问.

据我所知,辅助线程和主线程之间的链接可以通过PostThreadMessage()API来实现.


如果我错了,请告诉我该链接如何实现.我的意思是,辅助线程如何将数据发送到主线程.

请尽快帮我.在此先感谢:)

Hi,
Can any one please clarify a doubt as below.

In my knowledge, the link between the Secondary-Thread and Main-Thread can happens through PostThreadMessage() API.


If i''m wrong, then kindly please tell me, how this link can possible. I mean ,how the Secondary-Thread can send data to Main-Thread.

Please help me soon. Thanks in advance :)

推荐答案

PostThreadMessage的调用是正确的,但前提是您的主线程运行Windows UI且主循环使用GetMessage-TranslateMessage-DispatchMessage循环就像功能WinMain通常一样.

当您将其发送到主线程后仍然无法获得所需的内容时,我假设您的主线程没有执行此操作.您不能将消息调度到任意线程;您应该为接收消息的线程编写一个特殊的代码.做到这一点的一种好方法是建立阻塞消息队列.阻塞的接收线程将被OS置于等待状态,并且永远不会调度回执行,直到唤醒下一条消息或终止为止.

不幸的是,我只有这种机制的C#代码,而没有C ++.但是您可以看一下它,因为它很清楚,可以理解这个主意: ^ ].

代替EventWaitHandle,您将需要使用Windows事件对象,请参见 http ://msdn.microsoft.com/zh-CN/library/ms682655%28v=vs.85%29.aspx [
The call to PostThreadMessage is right thing, but only if your main thread runs Windows UI with the main cycle using GetMessage-TranslateMessage-DispatchMessage cycle as the function WinMain typically does.

As you''re sending it to a main thread and still cannot get what you want, I assume your main thread does not do it. You cannot dispatch message to a arbitrary thread; you should write a special code for the thread accepting messages. One good way of doing this is making a blocking message queue. A receiving thread, being blocked, will be put to a wait state by OS and never scheduled back to execution until awaken be a next message or terminated.

Unfortunately, I only have C# code for such mechanism, not C++. But you can look it it, because it''s clear enough to get the idea: http://www.codeproject.com/Tips/149540/Simple-Blocking-Queue-for-Thread-Communication-and[^].

Instead of EventWaitHandle you will need to use Windows Event object, see http://msdn.microsoft.com/en-us/library/ms682655%28v=vs.85%29.aspx[^].

—SA


好吧,您始终可以将用户或注册的消息发布到Windows句柄中……但是您将不得不以某种方式来管理内存.

否则,您可以使用管道或任何受保护的资源在辅助线程和主线程之间进行通信.

另一种可能性是使用COM/ActiveX来管理线程.

最后的可能性可能是编译一个混合模式的应用程序,然后使用.NET使其运行良好.

对于现有的大型应用程序,所有这些解决方案都可能难以实施,并且会花费很多时间...

但是,如果可以从线程受益的领域定义明确并且可以隔离,那么提出解决方案就不会那么困难.
Well, you can always post user or registered messages to a windows handle... but you will then have to somehow manage the memory.

Otherwise, you can uses pipe or any protected resources to communicate between the secondary and main thread.

Another possibility is to uses COM/ActiveX to manage threading.

A final possibility might be to compile a mixed-mode application and then uses .NET for things it does well.

For a large existing application, all of those solutions might be hard to implement and take much time...

But if the area that would benefit from threading is well-defined and can be isolated, then it should not be that hard to come with a solution.


这篇关于在MFC多线程编程中,主线程和Seondary-Thread之间的链接如何发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆