MFC Controls and Thread Safety :: MFC

Started by
0 comments, last by kuphryn 21 years, 4 months ago
Hi. I read an interesting thread at a MSDN newgroup on MFC and thread safety. Its author asks about why a thread causes the process to hang when you pass to the thread points to MFC control objects (CListrCtrl, CEdit, CTreeCtrl, etc). I remember something similar to what the author of thread asks from a application I just finished. I found that you can make changes to MFC controls from threads via pointers to the controls. Everything works well until you call WaitForSingleObject(), or WaitForMultipleObject(), etc. In other words, the process hangs if you call WaitForSingleObject() when the thread is updating an MFC control. Ivan Lalis posted an interesting solution involving MFC controls handles. He writes that one solution is to pass in the worker threads handles to MFC controls instead of pointers.
    
UINT theWorkerThread(LPVOID pParam)
{
   CListCtrl theList;
   theList.attach(handleToRealListbox);
   ...
   theList.detach();
   return 0;
}
    
I would like to know what is the safest solution to this problem? Secondly, in terms of Lalis' solution, do you create and attach, for example, one CListCtrland for the entire life-time of the thread? Thanks, Kuphryn [edited by - kuphryn on December 6, 2002 12:56:36 PM]
Advertisement
because mfc handle maps are thread-specific, and aren''t shareable across threads. so you have to use raw handles for interthread communication.

This topic is closed to new replies.

Advertisement