Solved

Accessing view object from launched thread

Posted on 1997-11-21
2
356 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

20 Experts available now in Live!

Get 1:1 Help Now