[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Multithreading with MFC objects

Posted on 1997-10-06
3
Medium Priority
?
290 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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