Solved

Accessing view object from launched thread

Posted on 1997-11-21
2
358 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
ID: 1310517
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
ID: 1310518
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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