Solved

Multithreading with MFC objects

Posted on 1997-10-06
3
232 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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
jtwine100697 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now