AfxGetMainWnd()在线程里使用的问题

AfxGetMainWnd()在线程里使用的问题

AfxGetMainWnd()的使用依赖于线程
具体参加源代码

 1 _AFXWIN_INLINE CWnd *  AFXAPI AfxGetMainWnd()
 2 { CWinThread* pThread = AfxGetThread();
 3   return pThread != NULL ? pThread->GetMainWnd() : NULL; }

 4
 5 // 而AfxGetThread获取的是当前线程,而不是主线程!
 6 CWinThread *  AFXAPI AfxGetThread()
 7 {
 8// check for current thread in module thread state
 9AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
10CWinThread* pThread = pState->m_pCurrentWinThread;
11return pThread;
12}

13
所以在非主线程里使用可能会有问题,解决方法,在新创建的线程里使用AfxGetApp()->m_pMainWnd;

你可能感兴趣的:(AfxGetMainWnd()在线程里使用的问题)