?
Solved

Multithreading with MFC objects

Posted on 1997-10-06
3
Medium Priority
?
279 Views
Last Modified: 2013-11-20
How do you access to an MFC (CImageList, CMenu, etc.) from multiple threads? Can MFC objects be shared among multiple threads? If No, what are the workarounds? Any example will be helpful.
0
Comment
Question by:ames
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
jtwine100697 earned 600 total points
ID: 1307564
Not all of the MFC classes can be directly used by multiple threads.  CWnd, and its derived classes is one of the ones that cannot.

Two MFC objects, in different threads, can reference the same control/window, however.  At least, I never have had a problem.

When using MFC objects that encapsulate (or use) standard Windows controls, you pass the m_hWnd (HWND) of the object to the thread, and have the thread create a new object, and then attach it to the passed HWND.

For example, say that you want to fill a CListCtrl when your application first comes up, but want to do it in a thread.  Pass the thread the m_hWnd of the CListCtrl member that you have.  The thread can safely create a new CListCtrl object and Attach() it to the HWND of the control.  The thread can then safely manipulate the control.

You also need to do this for any objects that use any windowd/controls internally, like CAsyncSocket.  Pass the m_hSocket (HSOCKET) to the thread, and have the thread create and attach to the passed handle.

-=- James.
0
 

Author Comment

by:ames
ID: 1307565
I hope your anwer is right and so I give you full point. But I had problem with CAsyncSocket object sharing among multiple threads. It gives me an error which says something like can not be shared among multiple threads. Do you have any example on CAsyncSocket? I'll appreciate it.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1307566
OK...

The actual CAsyncSocket object is not the problem, it is the CWnd that is uses for its notifications.  The CWnd object cannot be handed between threads.

If you want to share this object between threads, here is what you need to do:

o Use Detach() to get the CAsyncSocket's handle (HSOCKET).
o Pass the HSOCKET to the new thread.
o Have the new thread create a CAsyncSocket and Attach() it to the passed HSOCKET.

There you go!  

That is how I operate a small server process that Accept()s connections, and then hands the connected socket off to a new thread to process it.

Email me personally, (jtwine@bettynet.com) and I can give you some code snippets, if need be.

-=- James.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

743 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