Link to home
Start Free TrialLog in
Avatar of Sam_Barham
Sam_Barham

asked on

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)
Avatar of mikeblas
mikeblas

> call fails

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

Does the graphic thread have a message queue?

..B ekiM
Avatar of Sam_Barham

ASKER

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.
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
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")
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


 > 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
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
ASKER CERTIFIED SOLUTION
Avatar of anoops
anoops

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> 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
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.