Solved

Accessing view object from launched thread

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 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