Re: List Control Thread Access

Tech-Archive recommends: Fix windows errors by optimizing your registry



First of all your thread is a worker thread, and can't process GUI stuff.
Second, and probably the reason your program is crashing is that MFC object
are not thread safe, which means that you can't simply pass a list control
object to another thread and expect it to work (the message pump from MFC is
not thread safe).

The correct way to solve your problem is to create a GUI thread, and post
messages back to the main thread (who owns the list control) and have it
update the list control.

Or you can simply pass the hWnd of the list control to the GUI thread, and
post messages to the list control to add items.

AliR.

"Phil" <spam@xxxxxxxxxxx> wrote in message
news:MPG.1cf6f2eefeb0dae0989791@xxxxxxxxxxxxxxxxxxxx
> I have a little problem trying to access a list control in a program
> from a thread.
>
> The main program is an MFC SDI app with a list control on the main view,
> then in the initialupdate I start a thread with the following code;
>
> (The thread is called via "AfxBeginThread(RunThread, this); ").
>
> UINT CProkNETView::RunThread(LPVOID p)
> {
> CProkNETView * me = (CProkNETView *)p;
> me->AutoUpdateThread();
> return 0;
> }
>
> void CProkNETView::AutoUpdateThread(void)
> {
> m_wndList.InsertItemEx(0,"text");
> }
>
>
> When "InsertItemEx" is called from the thread, the program crashes. When
> it is just called from a normal non threaded part of the program it
> works fine. Surely the "AutoUpdateThread" function should be able to
> access the list control fine, as it's still part of "CProkNETView" ?
>
> Am I missing something ?
>
>
>
> --
> Phil
>


.



Relevant Pages

  • Re: SetCheck
    ... from a list control when I changed a selection programatically. ... For the reasons I pointed out, Windows can't be changed; there are tens of millions of ... Windows that needs to be changed rather than MFC. ...
    (microsoft.public.vc.mfc)
  • Re: Problem populating a List Control from an ATL component
    ... MFC problems... ... Microsoft MVP, MCSD ... > I have programmed an MFC application that has a List Control on a dialog. ... > The list control is reached from the ATL component, ...
    (microsoft.public.vc.atl)
  • Re: Need some hints on app design
    ... about mfc yet it thought there might be some class, methods or functionality i could use. ... The "MFC way" is to design the views as the user's interface to data that is in, or fetched by, the CDocument. ... The fact that your "short results" view is holding the data in a list control instead of in the doc is a rather awkward fit with this architecture. ... But if you are sure that the short results view object will always exist when you want to access it, you can ask the document to give you a pointer to that view or even to give you the next/previous record tag. ...
    (microsoft.public.vc.mfc)
  • Re: CListCtrl Problem: Which class does MFC new for us, CEdit or CWnd???
    ... I encountered a problem today about MFC's CListCtrl. ... I added a list control from MFC, ... Someone told me MFC creates a CWnd class for us but not CEdit. ...
    (microsoft.public.vc.mfc)
  • Re: Problem populating a List Control from an ATL component
    ... "Jaime Stuardo" wrote in ... I need that list control to be filled in an ATL component ... > This is the code in the MFC dialog: ... LVM_GETITEMCOUNT works because it does not involve any pointers. ...
    (microsoft.public.vc.atl)