Re: writing to gui from thread
"Ajay Kalra" <ajaykalra@yahoo.com> wrote in message
news:1172072162.493377.198310@l53g2000cwa.googlegroups.com...
On Feb 19, 3:31 pm, kunal s patel
<kunalspa...@discussions.microsoft.com> wrote:
Hi all,
Here is a part of code:
#include "maindlg.h"
UINT MyThread(LPVOID obj); //thread function definition
class Cmaindlg
{
//this is the class of main gui with a listbox
CListbox m_listbox;
};
#include "sim.h"
#include "maindlg.h"
class sim
{
//another file
void proc(); //this function access the gui
void run();
}
Now when someone press a button in maindlg application i spawn a thread
like this:
UINT MyThread(LPVOID obj)
{
sim s;
s.proc();
}
The function proc is defined as
void sim::proc()
{
Cmaindlg* pmain = (Cmaindlg*) AfxGetMainWnd();
pmain->m_listbox.AddString("Hi");
}
Now when i try to run this application, i get an assertion error. I am
not
able to understand what is going wrong. Can anyone explain me the way
out of
it. Basically i want to access the gui objects from thread.
thanks
kunal
I think you have got the crux of the problem from other posters. In
your worker thread, dont call AfxGetMainWnd; instead send window
handle to the thread and use it later when you need it to
PostMessage() using the window handle.
---
Ajay
There's nothing wrong with calling AfxGetMainWnd() from a worker thread.
The problem is caused when you try to use the resulting CWnd*.
The reason is related to the handle maps that MFC maintains, for mapping
between Windows windows and CWnd objects. These maps are maintained on a
per-thread basis. So, if you try to use the CWnd* that's returned from
AfxGetMainWnd(), it's not pointing to the same CWnd object that actually
contains your main window. Instead, it points to some temparary CWnd object
in the worker thread, which doesn't even have a HWND. Hence, any calls that
require a HWND will assert. In your code, AddString() is a simple wrapper
over SendMessage( hWnd, LB_ADDSTRING...), which requires a HWND.
The advice in this thread is all the same: a worker thread is for work, not
for updating the UI. Update the UI from the GUI thread, not from a worker
thread.
Mike