alberthyc
July 29th, 2007, 07:43 PM
Hi, i created a MFC dialog and i wanted to use the multithreading.
I declared a global thread function, and i want to use the dialog class member variables or functions, say i want to use the ClistBox::AddString function in the global thread function, what's the syntax for that?
Arjay
July 29th, 2007, 09:45 PM
Essentially you can't manipulate MFC UI items from a secondary thread (although there are other approaches to achieve the same functionality). Read the MFC FAQ "MFC Thread: How to access UI elements from a thread in MFC" (http://www.codeguru.com/forum/showthread.php?t=312454).
Also, Check out the SimpleThread.zip sample in this post (http://www.codeguru.com/forum/showthread.php?p=1607352#post1607352). This sample shares a string between two threads and appends the string to an edit box. Although it doesn't append items to a list box, the sample could easily be modified to do so. Modify the OnLogRecieved message handler as follows:
// Message received when a log item is available (sent by secondary thread)
LRESULT CSimpleThreadDlg::OnLogReceived( WPARAM, LPARAM )
{
// Add the log entry try the list box
m_ListBox.AddString( m_LogMgr.GetLogString( ) );
return 1;
}