Accessing view object from launched thread

I am having trouble getting a copy of my CListView derived view from within a worker thread I launch from the view. I understand that one should not pass MFC objects to threads, so I am passing the handle to the view and trying to get access to the view via the FromHandlePermanent function but the call returns NULL.

Can anyone determine where I am going wrong here?
- from my CListView derived view class I launch the thread

AfxBeginThread(LoadThreadProc, m_hWnd);

- here is the thread
UINT LoadThreadProc( LPVOID pParam )
    HWND hWndView = (HWND)pParam;
    CMyListView* pView = (CMyListView*)

    if (pView == NULL ||
    return (UINT)-1;    // illegal parameter

    return 0;    // thread completed successfully
Who is Participating?
Tommy HuiConnect With a Mentor EngineerCommented:
Why not send the view a message with SendMessage? That's what I'm currently doing. Anything else and you're going to run into assertions with MFC because it really isn't designed for that.
That's right.  MFC code is not thread safe,
FromHandlePermanent probably works only on the current thread, but it realy doesn't matter because:
Creating or getting a CWnd for a window that is manipulated by a different thread is dangoures!  even a simple thing as reading the window's caption can cause your application to crush - if one thread reads and another writes!
Only the message queqe is protected via a semaphore, thats why you want to send messages ( preffers that carry dynamicly allocated data ).  if your code can be async. then it's even better to use the PostMessage().
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.