Posting messages between threads

I have two User Interface threads, onw doing some processing and the other drawing to a window.  I want the processing thread to send messages to the drawing thread. How do I let the processing thread know about the graphics thread so that it can send messages to it.  If I pass the processing thread a pointer to the graphics thread, the graphicThread->PostThreadMessage(DRAWMODEL, 0, 0) call fails (DRAWMODEL is a a message I register myself)
Sam_BarhamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikeblasCommented:
> call fails

In what way does it fail?  Does PostThreadMessage() return FALSE?

Does the graphic thread have a message queue?

..B ekiM
0
Sam_BarhamAuthor Commented:
Yes, PostThreadMessage returns FALSE, and yes, the graphics thread does have a message queue.  It is a CWinThread derived object started with the user-interface version of AfxBeginThread.
0
mikeblasCommented:
What is the value of DRAWMODEL?

 > It is a CWinThread derived object started with the user-interface

What does that mean? The one that takes a CRuntimeClass*, you mean?

 > version of AfxBeginThread.

That doesn't guarantee it has a message queue. Are you _sure_ it has a message queue? What makes you certain?

..B ekiM
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Sam_BarhamAuthor Commented:
SOrry, in that case I am not sure that it has a message queue, although it seems to create the drawing window just fine, how can I tell.

I don't know what the value of DRAWMODEL is, I just store the value from RegisterWindowMessage("DRAWMODEL")
0
mikeblasCommented:
Which operating system are you using?  What does GetLastError() return after PostThreadMessage() fails?

If the thread really is creating the window, then MFC is causing a message queue to be created for it.

..B ekiM
0
mikeblasCommented:


 > I don't know what the value of DRAWMODEL is, I
 > just store the value from RegisterWindowMessage("DRAWMODEL")

Huh?  Why can't you look at that stored value?

Why are you registering a message? You don't need to do that for a thread message.

..B ekiM
0
GlennDeanCommented:
Sam:
   Pass the processing thread the CWnd * ptr of the graphics thread (best to do this before you create the graphics thread).  Then you can just go
pCWnd->PostMessage(....);
   I've got the above scenario to work pretty good.  
   I think your usage of RegisterWindowMessage is a good idea.
   Glenn
0
anoopsCommented:
1).Just a comment on Glenn's answer..
>>Pass the processing thread the CWnd * >>ptr of the graphics thread..
 I believe( And I had read lotsa stuff regarding that..) it's really unsafe to pass CWnd* around among threads(Oh yea..because of thread affinity).
If you need to pass, then pass the HANDLE of the window and at the receiver end create a CWnd* using CWnd::FromHandle() function.
2)You don't have to register a message if you are going to do only inter thread(intra process ) communication.

Hence a combination of both Mike's and Glenn's suggestions would do the work.
i.e pass the handle of the Graphics thread( I guess it's the main thread..even otherwise this would work) to the processing thread, create a CWnd* from this handle and then use PostMessage() function of CWnd
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikeblasCommented:
> I think your usage of RegisterWindowMessage is a good idea.

Why?

It's only for messages that go across process boundaries, not thread boundaries. Thread messages, in particular, really allow you to use any ID for the message--except WM_TIMER.

RegisterWindowMessage() creates an atom of the name, and that uses system resources.

..B ekiM
0
SubramanyaCommented:
What i can tell you is
Have a globel bolean variable which is
acceble by both the threads.
Set the variable in one thread and
Use message handler like onidle in user
inteface thread ,check for the variable
there.Afcource this is not a very good solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.