Solved

Multithreading with MFC objects

Posted on 1997-10-06
3
246 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 69
Tab names are off by one 5 53
ffmpeg - "rtsp://...... Operation not permitted" 4 74
fizzArray2 challenge 1 72
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

16 Experts available now in Live!

Get 1:1 Help Now