使用AfxGetMainWnd函数获取MFC程序中的主框架类指针是一个常用作法。但是你会发现这一做法有时也会失灵。不信, 你测试一下下面的代码:
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
if (NULL!=pMainWnd)
{
CView *pView = pMainWnd->GetActiveView();
if (NULL!=pView)
{
CDC *pDC = pView->GetDC();
ASSERT(NULL!=pDC);
pDC->TextOut(100,100,_T("来自线程的字符串"));
pView->ReleaseDC(pDC);
}
}
return 0;
}
void CMainFrame::OnTest1()
{
// TODO: 在此添加命令处理程序代码
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
// Destroy the thread object.
CloseHandle( hThread );
}
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
if (NULL!=pMainWnd)
{
CView *pView = pMainWnd->GetActiveView();
if (NULL!=pView)
{
CDC *pDC = pView->GetDC();
ASSERT(NULL!=pDC);
pDC->TextOut(100,100,_T("来自线程的字符串"));
pView->ReleaseDC(pDC);
}
}
return 0;
}
void CMainFrame::OnTest1()
{
// TODO: 在此添加命令处理程序代码
HANDLE hThread;
unsigned threadID;
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
// Destroy the thread object.
CloseHandle( hThread );
}
运行OnTest1函数,你会发现客户区并没有打印"来自线程的字符串"。下面我们把线程函数变一下:
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
// CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
COwnerApp *pApp = (COwnerApp *)AfxGetApp();
CMainFrame* pMainWnd = (CMainFrame*) pApp->m_pMainWnd;
if (NULL!=pMainWnd)
{
CView *pView = pMainWnd->GetActiveView();
if (NULL!=pView)
{
CDC *pDC = pView->GetDC();
ASSERT(NULL!=pDC);
pDC->TextOut(100,100,_T("来自线程的字符串"));
pView->ReleaseDC(pDC);
}
}
return 0;
}
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
// CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
COwnerApp *pApp = (COwnerApp *)AfxGetApp();
CMainFrame* pMainWnd = (CMainFrame*) pApp->m_pMainWnd;
if (NULL!=pMainWnd)
{
CView *pView = pMainWnd->GetActiveView();
if (NULL!=pView)
{
CDC *pDC = pView->GetDC();
ASSERT(NULL!=pDC);
pDC->TextOut(100,100,_T("来自线程的字符串"));
pView->ReleaseDC(pDC);
}
}
return 0;
}
运行OnTest1函数,我们发现视图客户区出现了"来自线程的字符串"。接下来我们调试进去AfxGetMainWnd函数,发现AfxGetMainWnd函数如下:
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
{ CWinThread* pThread = AfxGetThread();
return pThread != NULL ? pThread->GetMainWnd() : NULL; }
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
{ CWinThread* pThread = AfxGetThread();
return pThread != NULL ? pThread->GetMainWnd() : NULL; }
由于AfxGetThread()函数返回为NULL,所以AfxGetMainWnd函数返回为NULL。为什么会这样呢?下面我提出我的猜想(本人暂时验证不了,仅起抛砖引玉的作用)。我估计是MFC在多线程中大量运用了TLS(线程本地存储)来保存某些状态,主框架窗口指针属于主线程的TLS(线程本地存储)保存的状态,但是应用程序类指针不属于TLS保存的状态,它可以在该进程的任何线程获取。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/clever101/archive/2010/03/11/5372116.aspx