Solved

Accessing view object from launched thread

Posted on 1997-11-21
2
355 Views
Last Modified: 2013-11-20
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*)
        CWnd::FromHandlePermanent(hWndView);
      

    if (pView == NULL ||
        !pView->IsKindOf(RUNTIME_CLASS(CMyListView)))
    return (UINT)-1;    // illegal parameter


    return 0;    // thread completed successfully
}
0
Comment
Question by:rascal
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 20 total points
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:shaig
Comment Utility
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().
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 47
Running shell command from Access 3 63
has77  challenge 9 67
sumHeights2  challenge 7 75
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 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