Re: Multithtreaded access to GUI controls
"K?r?at" <xx@yy.com> wrote in message
news:O0SC4aeeIHA.2448@TK2MSFTNGP03.phx.gbl...
Hi,
In my code I have a method which is called by multiple non-gui threads.
The method simply writes given 'int' to a TextBox.
void writeInt (int nInt)
{
EnterCriticalSection (&cs);
m_PDlg->SetDlgItemInt (IDC_EDIT1, nInt);
LeaveCriticalSection (&cs);
}
I also synchronize execution of that method. Now multiple non-gui threads
can -?safely?- change content of the TextBox. Is this enough protection?
Is it legal to directly call a GUI method from non-gui threads? I have no
way to sync the GUI thread and my non-gui threads, does this cause a
problem?
No no no. This can deadlock your program. When you execute this code from
a secondary thread Windows executes SendMessage to the main thread. If main
thread happens to be waiting for the same critical section then it will not
accept the sent message and your program is dead.
Do not access any window from a thread that did not create it. See the
references others have provided, and this example:
http://vcfaq.mvps.org/mfc/12.htm
--
Scott McPhillips [VC++ MVP]