pMainWnd

 

CWinThread::m_pMainWnd

Remarks

Use this data member to store a pointer to your thread’s main window object. The Microsoft Foundation Class Library will automatically terminate your thread when the window referred to by m_pMainWnd is closed. If this thread is the primary thread for an application, the application will also be terminated. If this data member is NULL, the main window for the application’s CWinApp object will be used to determine when to terminate the thread. m_pMainWnd is a public variable of type CWnd*.

Typically, you set this member variable when you override InitInstance. In a worker thread, the value of this data member is inherited from its parent thread.

 

重要特征:
     如果由该成员函数所引用的窗口被关闭的话,MFC库将自动的终止CWinThread对象所代表的线程。

例子说明:

  1. BOOL CTestApp::InitInstance()   
  2. {   
  3.     AfxEnableControlContainer();   
  4.   
  5.     // Standard initialization   
  6.     // If you are not using these features and wish to reduce the size   
  7.     //  of your final executable, you should remove from the following   
  8.     //  the specific initialization routines you do not need.   
  9.   
  10. #ifdef _AFXDLL   
  11.     Enable3dControls();         // Call this when using MFC in a shared DLL   
  12. #else   
  13.     Enable3dControlsStatic();   // Call this when linking to MFC statically   
  14. #endif   
  15.   
  16.     CTestDlg dlg;   
  17.     m_pMainWnd = &dlg;   ①
  18.     int nResponse = dlg.DoModal();   
  19.     if (nResponse == IDOK)   
  20.     {   
  21.         // TODO: Place code here to handle when the dialog is   
  22.         //  dismissed with OK   
  23.     AfxMessageBox(dlg.m_strEdit);   ②
  24.     }   
  25.     else if (nResponse == IDCANCEL)   
  26.     {   
  27.         // TODO: Place code here to handle when the dialog is   
  28.         //  dismissed with Cancel   
  29.     }   
  30.   
  31.     // Since the dialog has been closed, return FALSE so that we exit the   
  32.     //  application, rather than start the application's message pump.   
  33.     return FALSE;   
  34. }  

由于我们的目的是为了在单击对话框的确定按钮过后弹出对话框,如果直接按如上的代码运行,对话框是看不到的。就只有把1处的代码注释掉才能正常运行。

原理:

   正是因为m_pMainWnd的重要特性,所以如果不注释掉1处,无论单击哪个按钮,应用程式的主线程都将被自动终止,之后2处的代码当然不会得到执行。为了达到我要的效果,因此不应该将dlg对象的指针赋予成员变量m_pMainWnd,从而只能把1处删除。

  

 

你可能感兴趣的:(thread,Microsoft,mfc)