Re: writing to gui from thread
kunal s patel schrieb:
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.
MFC window objects can only be used in the same thread that created them. Since
the m_listbox was created by the main GUI thread it can not be used from another
thread.
In addition, doing user interaction from a thread is a bad idea and opens the
door to all kind of bugs and problems. The assert you hit is only one of them.
Your worker thread should post (not send) a message to the main thread telling
it to add the string to the list box.
Norbert