MFC 多线程调用UpdateData崩溃

MFC 程序中使用多线程函数,使用UpdateData();更新界面数据时,会出现崩溃现象,

CWinThread* AfxBeginThread(AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
nPriority=THREAD_PRIORITY_NORMAL,
UINT nStackSize=0,
DWORD dwCreateFlags=0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL);

此时,个人的解决办法是,使用消息机制更新,实现方法如下所示:

#define WM_UPDATEDATA WM_USER+100

ON_MESSAGE(WM_UPDATEDATA, OnUpdateData)

LRESULT OnUpdateData(WPARAM wParam, LPARAM lParam);

LRESULT CMFCThreadDlg::OnUpdateData(WPARAM wParam, LPARAM lParam)
{
    UpdateData(wParam);
    return 1;
}
 

dlg.SendMeaasge(WM_UPDATEDATA,true,0);

此时,程序运行正常

你可能感兴趣的:(mfc,c++)