Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :)
Posted
Comments
Philippe Mori 17-Sep-11 11:51am    
What kind of communication do you need between your thread? Do you want to pass a progress percentage only, new data regulary (say every 0.25 second) or the final result when the thread is ready. Does the data is large?

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
 
Share this answer
 
Comments
Philippe Mori 17-Sep-11 11:46am    
I have given some extra ideas... Effectively, threading is a thing that is much easier to do in .NET.
Sergey Alexandrovich Kryukov 18-Sep-11 14:29pm    
Thank you, Philippe.
I commented on your solution, please see.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Sep-11 14:29pm    
Yes, this is a valid set of directions, my 5. You advice is somewhat vague, but this is mostly because OP does not share the goal of all this activity; in this case we could probably advise less variety of more specific ways, but in greater detail.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900