PostMessage from WorkerThread to Main Window in MFC
CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
may return before the thread starts. This causes your ThreadParams
to go out of scope, so when you access it from the thread, you are accessing freed memory. You need to allocate it some other way, such as:
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1(){ ThreadParams* params = new ThreadParams(); params->m_hWnd = m_hWnd; params->FTPHost = "test_host"; params->FTPUsername = "test"; params->FTPPassword = "test"; AfxBeginThread(FTPConnectThread,params);}//child thread functionUINT FTPConnectThread( LPVOID pParam ){ if(pParam == NULL) { return 0; } ThreadParams *params = (ThreadParams*)pParam; OutputDebugString(params->FTPHost); Sleep(4000); //simulating a network call CString * message = new CString("Conencted"); PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message); delete params; return 1;}