[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Posting messages between threads

Posted on 2000-02-03
10
Medium Priority
?
167 Views
Last Modified: 2013-11-20
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)
0
Comment
Question by:Sam_Barham
10 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 2488163
> call fails

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

Does the graphic thread have a message queue?

..B ekiM
0
 

Author Comment

by:Sam_Barham
ID: 2488171
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2488178
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 

Author Comment

by:Sam_Barham
ID: 2488190
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2488586
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2488591


 > 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
 
LVL 3

Expert Comment

by:GlennDean
ID: 2488671
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
 
LVL 1

Accepted Solution

by:
anoops earned 150 total points
ID: 2489257
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
 
LVL 11

Expert Comment

by:mikeblas
ID: 2490332
> 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
 

Expert Comment

by:Subramanya
ID: 2491307
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question