MFC Controls and Thread Safety :: MFC

This is a discussion on MFC Controls and Thread Safety :: MFC within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I read an interesting thread at a MSDN newgroup on MFC and thread safety. Its author asks about why ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    MFC Controls and Thread Safety :: MFC

    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.

    Code:
    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
    Last edited by kuphryn; 12-06-2002 at 11:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tab controls - MFC (revived)
    By Robert602 in forum Windows Programming
    Replies: 1
    Last Post: 01-22-2002, 12:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21