Solved

Multithreading with MFC objects

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 178
Generic progress indicator 6 117
countEvens challenge 2 100
withoutTen challenge 14 121
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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