CWinThread::m_pMainWnd 该成员变量去存储你的线程主窗口对象。当和m_pMainWnd 相关的窗口被关闭后,MFC会自动终止你的线程。如果该线程是应用程序主线程,程序也将会被终止。如果该数据成员为NULL,应用程序CWinApp对象的主窗口将用来决定什么时候去终止线程。m_pMainWnd是一个CWnd*类型的public变量。 很明显,你需要在重载InitInstance时为m_pMainWnd赋值。在工作线程中,m_pMainWnd自动继承其父线程的值。 补充:只有CWinThread对象才有m_pMainWnd class CWinThread : public CCmdTarget { DECLARE_DYNAMIC(CWinThread) public: // Constructors CWinThread(); BOOL CreateThread(DWORD dwCreateFlags = 0, UINT nStackSize = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL); // Attributes CWnd* m_pMainWnd; // main window (usually same AfxGetApp()->m_pMainWnd) CWnd* m_pActiveWnd; // active main window (may not be m_pMainWnd) BOOL m_bAutoDelete; // enables 'delete this' after thread termination .............. } //在用户界面线程中创建非模态对话框 if (!pDlg.Create(IDD_DIALOG1, NULL)) { AfxMessageBox("窗口创建失败!"); } else { pDlg.ShowWindow(SW_SHOW); m_pMainWnd=&pDlg; } 在创建用户界面线程时,该句一定要加上。否则在线程不会得到释放! |