1、创建和终止线程

  在MFC程序中创建一个线程,宜调用AfxBeginThread函数。该函数因参数不同而具有两种重载版本,分别对应工作者线程和用户接口(UI)线程。

  工作者线程

CWinThread *AfxBeginThread(
 AFX_THREADPROC pfnThreadProc, //控制函数
 LPVOID pParam, //传递给控制函数的参数
 int nPriority = THREAD_PRIORITY_NORMAL, //线程的优先级
 UINT nStackSize = 0, //线程的堆栈大小
 DWORD dwCreateFlags = 0, //线程的创建标志
 LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL //线程的安全属性
);

  工作者线程编程较为简单,只需编写线程控制函数和启动线程即可。下面的代码给出了定义一个控制函数和启动它的过程:

//线程控制函数
UINT MfcThreadProc(LPVOID lpParam)
{
 CExampleClass *lpObject = (CExampleClass*)lpParam;
 if (lpObject == NULL || !lpObject->IsKindof(RUNTIME_CLASS(CExampleClass)))
  return - 1; //输入参数非法 
 //线程成功启动
 while (1)
 {
  ...//
 }
 return 0;
}

//在MFC程序中启动线程
AfxBeginThread(MfcThreadProc, lpObject);

  UI线程

  创建用户界面线程时,必须首先从CWinThread 派生类,并使用 DECLARE_DYNCREATE 和 IMPLEMENT_DYNCREATE 宏声明此类。

  下面给出了CWinThread类的原型(添加了关于其重要函数功能和是否需要被继承类重载的注释):

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

  // only valid while running
  HANDLE m_hThread; // this thread's HANDLE
  operator HANDLE() const;
  DWORD m_nThreadID; // this thread's ID

  int GetThreadPriority();
  BOOL SetThreadPriority(int nPriority);

  // Operations
  DWORD SuspendThread();
  DWORD ResumeThread();
  BOOL PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam);

  // Overridables
  //执行线程实例初始化,必须重写
  virtual BOOL InitInstance();

  // running and idle processing
  //控制线程的函数,包含消息泵,一般不重写
  virtual int Run();

  //消息调度到TranslateMessage和DispatchMessage之前对其进行筛选,
  //通常不重写
  virtual BOOL PreTranslateMessage(MSG* pMsg);

  virtual BOOL PumpMessage(); // low level message pump

  //执行线程特定的闲置时间处理,通常不重写
  virtual BOOL OnIdle(LONG lCount); // return TRUE if more idle processing
  virtual BOOL IsIdleMessage(MSG* pMsg); // checks for special messages

  //线程终止时执行清除,通常需要重写
  virtual int ExitInstance(); // default will 'delete this'

  //截获由线程的消息和命令处理程序引发的未处理异常,通常不重写
  virtual LRESULT ProcessWndProcException(CException* e, const MSG* pMsg);

  // Advanced: handling messages sent to message filter hook
  virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);

  // Advanced: virtual access to m_pMainWnd
  virtual CWnd* GetMainWnd();

  // Implementation
 public:
  virtual ~CWinThread();
  #ifdef _DEBUG
   virtual void AssertValid() const;
   virtual void Dump(CDumpContext& dc) const;
   int m_nDisablePumpCount; // Diagnostic trap to detect illegal re-entrancy
  #endif
  void CommonConstruct();
  virtual void Delete();
  // 'delete this' only if m_bAutoDelete == TRUE

  // message pump for Run
  MSG m_msgCur; // current message

 public:
  // constructor used by implementation of AfxBeginThread
  CWinThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam);

  // valid after construction
  LPVOID m_pThreadParams; // generic parameters passed to starting function
  AFX_THREADPROC m_pfnThreadProc;

  // set after OLE is initialized
  void (AFXAPI* m_lpfnOleTermOrFreeLib)(BOOL, BOOL);
  COleMessageFilter* m_pMessageFilter;

 protected:
  CPoint m_ptCursorLast; // last mouse position
  UINT m_nMsgLast; // last mouse message
  BOOL DispatchThreadMessageEx(MSG* msg); // helper
  void DispatchThreadMessage(MSG* msg); // obsolete
};

  启动UI线程的AfxBeginThread函数的原型为:

CWinThread *AfxBeginThread(
 //从CWinThread派生的类的 RUNTIME_CLASS
 CRuntimeClass *pThreadClass, 
 int nPriority = THREAD_PRIORITY_NORMAL, 
 UINT nStackSize = 0, 
 DWORD dwCreateFlags = 0,
 LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);

  我们可以方便地使用VC++ 6.0类向导定义一个继承自CWinThread的用户线程类。下面给出产生我们自定义的CWinThread子类CMyUIThread的方法。

  打开VC++ 6.0类向导,在如下窗口中选择Base Class类为CWinThread,输入子类名为CMyUIThread,点击"OK"按钮后就产生了类CMyUIThread。

其源代码框架为:

/////////////////////////////////////////////////////////////////////////////
// CMyUIThread thread

class CMyUIThread : public CWinThread
{
 DECLARE_DYNCREATE(CMyUIThread)
 protected:
  CMyUIThread(); // protected constructor used by dynamic creation

  // Attributes
 public:

  // Operations
 public:

  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CMyUIThread)
  public:
   virtual BOOL InitInstance();
   virtual int ExitInstance();
  //}}AFX_VIRTUAL

  // Implementation
 protected:
  virtual ~CMyUIThread();

  // Generated message map functions
  //{{AFX_MSG(CMyUIThread)
   // NOTE - the ClassWizard will add and remove member functions here.
  //}}AFX_MSG

 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CMyUIThread

IMPLEMENT_DYNCREATE(CMyUIThread, CWinThread)

CMyUIThread::CMyUIThread()
{}

CMyUIThread::~CMyUIThread()
{}

BOOL CMyUIThread::InitInstance()
{
 // TODO: perform and per-thread initialization here
 return TRUE;
}

int CMyUIThread::ExitInstance()
{
 // TODO: perform any per-thread cleanup here
 return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMyUIThread, CWinThread)
//{{AFX_MSG_MAP(CMyUIThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

  使用下列代码就可以启动这个UI线程:

CMyUIThread *pThread;
pThread = (CMyUIThread*)
AfxBeginThread( RUNTIME_CLASS(CMyUIThread) );

  另外,我们也可以不用AfxBeginThread 创建线程,而是分如下两步完成:

  (1)调用线程类的构造函数创建一个线程对象;

  (2)调用CWinThread::CreateThread函数来启动该线程。

  在线程自身内调用AfxEndThread函数可以终止该线程:

void AfxEndThread(
 UINT nExitCode //the exit code of the thread
);

  对于UI线程而言,如果消息队列中放入了WM_QUIT消息,将结束线程。

  关于UI线程和工作者线程的分配,最好的做法是:将所有与UI相关的操作放入主线程,其它的纯粹的运算工作交给独立的数个工作者线程。

  候捷先生早些时间喜欢为MDI程序的每个窗口创建一个线程,他后来澄清了这个错误。因为如果为MDI程序的每个窗口都单独创建一个线程,在窗口进行切换的时候,将进行线程的上下文切换!

3.线程与消息队列

  在WIN32中,每一个线程都对应着一个消息队列。由于一个线程可以产生数个窗口,所以并不是每个窗口都对应着一个消息队列。下列几句话应该作为"定理"被记住:

  "定理" 一

  所有产生给某个窗口的消息,都先由创建这个窗口的线程处理;

  "定理" 二

  Windows屏幕上的每一个控件都是一个窗口,有对应的窗口函数。

  消息的发送通常有两种方式,一是SendMessage,一是PostMessage,其原型分别为:

LRESULT SendMessage(HWND hWnd, // handle of destination window
 UINT Msg, // message to send
 WPARAM wParam, // first message parameter
 LPARAM lParam // second message parameter
);
BOOL PostMessage(HWND hWnd, // handle of destination window
 UINT Msg, // message to post
 WPARAM wParam, // first message parameter
 LPARAM lParam // second message parameter
);

  两个函数原型中的四个参数的意义相同,但是SendMessage和PostMessage的行为有差异。SendMessage必须等待消息被处理后才返回,而PostMessage仅仅将消息放入消息队列。SendMessage的目标窗口如果属于另一个线程,则会发生线程上下文切换,等待另一线程处理完成消息。为了防止另一线程当掉,导致SendMessage永远不能返回,我们可以调用SendMessageTimeout函数:

LRESULT SendMessageTimeout(
 HWND hWnd, // handle of destination window
 UINT Msg, // message to send
 WPARAM wParam, // first message parameter
 LPARAM lParam, // second message parameter
 UINT fuFlags, // how to send the message
 UINT uTimeout, // time-out duration
 LPDWORD lpdwResult // return value for synchronous call
);

  4. MFC线程、消息队列与MFC程序的"生死因果"

  分析MFC程序的主线程启动及消息队列处理的过程将有助于我们进一步理解UI线程与消息队列的关系,为此我们需要简单地叙述一下MFC程序的"生死因果"(侯捷:《深入浅出MFC》)。

  使用VC++ 6.0的向导完成一个最简单的单文档架构MFC应用程序MFCThread:

  (1) 输入MFC EXE工程名MFCThread;

  (2) 选择单文档架构,不支持Document/View结构;

  (3) ActiveX、3D container等其他选项都选择无。

  我们来分析这个工程。下面是产生的核心源代码:

  MFCThread.h 文件

class CMFCThreadApp : public CWinApp
{
 public:
  CMFCThreadApp();

  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CMFCThreadApp)
   public:
    virtual BOOL InitInstance();
  //}}AFX_VIRTUAL

  // Implementation

 public:
  //{{AFX_MSG(CMFCThreadApp)
   afx_msg void OnAppAbout();
   // NOTE - the ClassWizard will add and remove member functions here.
   // DO NOT EDIT what you see in these blocks of generated code !
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

  MFCThread.cpp文件

CMFCThreadApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMFCThreadApp initialization

BOOL CMFCThreadApp::InitInstance()
{
 …
 CMainFrame* pFrame = new CMainFrame;
 m_pMainWnd = pFrame;

 // create and load the frame with its resources
 pFrame->LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,NULL);
 // The one and only window has been initialized, so show and update it.
 pFrame->ShowWindow(SW_SHOW);
 pFrame->UpdateWindow();

 return TRUE;
}

  MainFrm.h文件

#include "ChildView.h"

class CMainFrame : public CFrameWnd
{
 public:
  CMainFrame();
 protected: 
  DECLARE_DYNAMIC(CMainFrame)

  // Attributes
 public:

  // Operations
 public:
  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CMainFrame)
   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
   virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
  //}}AFX_VIRTUAL

  // Implementation
 public:
  virtual ~CMainFrame();
  #ifdef _DEBUG
   virtual void AssertValid() const;
   virtual void Dump(CDumpContext& dc) const;
  #endif
  CChildView m_wndView;

  // Generated message map functions
 protected:
 //{{AFX_MSG(CMainFrame)
  afx_msg void OnSetFocus(CWnd *pOldWnd);
  // NOTE - the ClassWizard will add and remove member functions here.
  // DO NOT EDIT what you see in these blocks of generated code!
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

  MainFrm.cpp文件

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
 //{{AFX_MSG_MAP(CMainFrame)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  // DO NOT EDIT what you see in these blocks of generated code !
  ON_WM_SETFOCUS()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
 // TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
 if( !CFrameWnd::PreCreateWindow(cs) )
  return FALSE;
  // TODO: Modify the Window class or styles here by modifying
  // the CREATESTRUCT cs

 cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
 cs.lpszClass = AfxRegisterWndClass(0);
 return TRUE;
}

  ChildView.h文件

// CChildView window

class CChildView : public CWnd
{
 // Construction
 public:
  CChildView();

  // Attributes
 public:
  // Operations
 public:
  // Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CChildView)
   protected:
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
  //}}AFX_VIRTUAL

  // Implementation
 public:
  virtual ~CChildView();

  // Generated message map functions
 protected:
  //{{AFX_MSG(CChildView)
   afx_msg void OnPaint();
  //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

ChildView.cpp文件
// CChildView

CChildView::CChildView()
{}

CChildView::~CChildView()
{}

BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
 if (!CWnd::PreCreateWindow(cs))
  return FALSE;

 cs.dwExStyle |= WS_EX_CLIENTEDGE;
 cs.style &= ~WS_BORDER;
 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,::LoadCursor(NULL, IDC_ARROW),
HBRUSH(COLOR_WINDOW+1),NULL);

 return TRUE;
}

void CChildView::OnPaint() 
{
 CPaintDC dc(this); // device context for painting

 // TODO: Add your message handler code here
 // Do not call CWnd::OnPaint() for painting messages
}

  文件MFCThread.h和MFCThread.cpp定义和实现的类CMFCThreadApp继承自CWinApp类,而CWinApp类又继承自CWinThread类(CWinThread类又继承自CCmdTarget类),所以CMFCThread本质上是一个MFC线程类,下图给出了相关的类层次结构:

我们提取CWinApp类原型的一部分:

class CWinApp : public CWinThread
{
 DECLARE_DYNAMIC(CWinApp)
 public:
  // Constructor
  CWinApp(LPCTSTR lpszAppName = NULL);// default app name
  // Attributes
  // Startup args (do not change)
  HINSTANCE m_hInstance;
  HINSTANCE m_hPrevInstance;
  LPTSTR m_lpCmdLine;
  int m_nCmdShow;
  // Running args (can be changed in InitInstance)
  LPCTSTR m_pszAppName; // human readable name
  LPCTSTR m_pszExeName; // executable name (no spaces)
  LPCTSTR m_pszHelpFilePath; // default based on module path
  LPCTSTR m_pszProfileName; // default based on app name

  // Overridables
  virtual BOOL InitApplication();
  virtual BOOL InitInstance();
  virtual int ExitInstance(); // return app exit code
  virtual int Run();
  virtual BOOL OnIdle(LONG lCount); // return TRUE if more idle processing
  virtual LRESULT ProcessWndProcException(CException* e,const MSG* pMsg);

 public:
  virtual ~CWinApp();
 protected:
  DECLARE_MESSAGE_MAP()
};

  SDK程序的WinMain 所完成的工作现在由CWinApp 的三个函数完成:

virtual BOOL InitApplication();
virtual BOOL InitInstance();
virtual int Run();

  "CMFCThreadApp theApp;"语句定义的全局变量theApp是整个程式的application object,每一个MFC 应用程序都有一个。当我们执行MFCThread程序的时候,这个全局变量被构造。theApp 配置完成后,WinMain开始执行。但是程序中并没有WinMain的代码,它在哪里呢?原来MFC早已准备好并由Linker直接加到应用程序代码中的,其原型为(存在于VC++6.0安装目录下提供的APPMODUL.CPP文件中):

extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
 // call shared/exported WinMain
 return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

  其中调用的AfxWinMain如下(存在于VC++6.0安装目录下提供的WINMAIN.CPP文件中):

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
 ASSERT(hPrevInstance == NULL);

 int nReturnCode = -1;
 CWinThread* pThread = AfxGetThread();
 CWinApp* pApp = AfxGetApp();

 // AFX internal initialization
 if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
  goto InitFailure;

 // App global initializations (rare)
 if (pApp != NULL && !pApp->InitApplication())
  goto InitFailure;

 // Perform specific initializations
 if (!pThread->InitInstance())
 {
  if (pThread->m_pMainWnd != NULL)
  {
   TRACE0("Warning: Destroying non-NULL m_pMainWnd/n");
   pThread->m_pMainWnd->DestroyWindow();
  }
  nReturnCode = pThread->ExitInstance();
  goto InitFailure;
 }
 nReturnCode = pThread->Run();

 InitFailure:
 #ifdef _DEBUG
  // Check for missing AfxLockTempMap calls
  if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
  {
   TRACE1("Warning: Temp map lock count non-zero (%ld)./n",
AfxGetModuleThreadState()->m_nTempMapLock);
  }
  AfxLockTempMaps();
  AfxUnlockTempMaps(-1);
 #endif

 AfxWinTerm();
 return nReturnCode;
}

  我们提取主干,实际上,这个函数做的事情主要是:

CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
pApp->InitApplication()
pThread->InitInstance()
pThread->Run();

  其中,InitApplication 是注册窗口类别的场所;InitInstance是产生窗口并显示窗口的场所;Run是提取并分派消息的场所。这样,MFC就同WIN32 SDK程序对应起来了。CWinThread::Run是程序生命的"活水源头"(侯捷:《深入浅出MFC》,函数存在于VC++ 6.0安装目录下提供的THRDCORE.CPP文件中):

// main running routine until thread exits
int CWinThread::Run()
{
 ASSERT_VALID(this);

 // for tracking the idle time state
 BOOL bIdle = TRUE;
 LONG lIdleCount = 0;

 // acquire and dispatch messages until a WM_QUIT message is received.
 for (;;)
 {
  // phase1: check to see if we can do idle work
  while (bIdle && !::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE))
  {
   // call OnIdle while in bIdle state
   if (!OnIdle(lIdleCount++))
    bIdle = FALSE; // assume "no idle" state
  }

  // phase2: pump messages while available
  do
  {
   // pump message, but quit on WM_QUIT
   if (!PumpMessage())
    return ExitInstance();

   // reset "no idle" state after pumping "normal" message
   if (IsIdleMessage(&m_msgCur))
   {
    bIdle = TRUE;
    lIdleCount = 0;
   }

  } while (::PeekMessage(&m_msgCur, NULL, NULL, NULL, PM_NOREMOVE));
 }
 ASSERT(FALSE); // not reachable
}

  其中的PumpMessage函数又对应于:

/////////////////////////////////////////////////////////////////////////////
// CWinThread implementation helpers

BOOL CWinThread::PumpMessage()
{
 ASSERT_VALID(this);

 if (!::GetMessage(&m_msgCur, NULL, NULL, NULL))
 {
  return FALSE;
 }

 // process this message
 if(m_msgCur.message != WM_KICKIDLE && !PreTranslateMessage(&m_msgCur))
 {
  ::TranslateMessage(&m_msgCur);
  ::DispatchMessage(&m_msgCur);
 }
 return TRUE;
}

  因此,忽略IDLE状态,整个RUN的执行提取主干就是:

do {
 ::GetMessage(&msg,...);
 PreTranslateMessage{&msg);
 ::TranslateMessage(&msg);
 ::DispatchMessage(&msg);
 ...
} while (::PeekMessage(...));

  由此,我们建立了MFC消息获取和派生机制与WIN32 SDK程序之间的对应关系。下面继续分析MFC消息的"绕行"过程。

  在MFC中,只要是CWnd 衍生类别,就可以拦下任何Windows消息。与窗口无关的MFC类别(例如CDocument 和CWinApp)如果也想处理消息,必须衍生自CCmdTarget,并且只可能收到WM_COMMAND消息。所有能进行MESSAGE_MAP的类都继承自CCmdTarget,如:

  MFC中MESSAGE_MAP的定义依赖于以下三个宏:

DECLARE_MESSAGE_MAP()

BEGIN_MESSAGE_MAP( 
 theClass, //Specifies the name of the class whose message map this is
 baseClass //Specifies the name of the base class of theClass
)

END_MESSAGE_MAP()

  我们程序中涉及到的有:MFCThread.h、MainFrm.h、ChildView.h文件

DECLARE_MESSAGE_MAP()
MFCThread.cpp文件
BEGIN_MESSAGE_MAP(CMFCThreadApp, CWinApp)
//{{AFX_MSG_MAP(CMFCThreadApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
MainFrm.cpp文件
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_SETFOCUS()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
ChildView.cpp文件
BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

  由这些宏,MFC建立了一个消息映射表(消息流动网),按照消息流动网匹配对应的消息处理函数,完成整个消息的"绕行"。

  看到这里相信你有这样的疑问:程序定义了CWinApp类的theApp全局变量,可是从来没有调用AfxBeginThread或theApp.CreateThread启动线程呀,theApp对应的线程是怎么启动的?

  答:MFC在这里用了很高明的一招。实际上,程序开始运行,第一个线程是由操作系统(OS)启动的,在CWinApp的构造函数里,MFC将theApp"对应"向了这个线程,具体的实现是这样的:

CWinApp::CWinApp(LPCTSTR lpszAppName)
{
 if (lpszAppName != NULL)
  m_pszAppName = _tcsdup(lpszAppName);
 else
  m_pszAppName = NULL;

 // initialize CWinThread state
 AFX_MODULE_STATE *pModuleState = _AFX_CMDTARGET_GETSTATE();
 AFX_MODULE_THREAD_STATE *pThreadState = pModuleState->m_thread;
 ASSERT(AfxGetThread() == NULL);
 pThreadState->m_pCurrentWinThread = this;
 ASSERT(AfxGetThread() == this);
 m_hThread = ::GetCurrentThread();
 m_nThreadID = ::GetCurrentThreadId();

 // initialize CWinApp state
 ASSERT(afxCurrentWinApp == NULL); // only one CWinApp object please
 pModuleState->m_pCurrentWinApp = this;
 ASSERT(AfxGetApp() == this);

 // in non-running state until WinMain
 m_hInstance = NULL;
 m_pszHelpFilePath = NULL;
 m_pszProfileName = NULL;
 m_pszRegistryKey = NULL;
 m_pszExeName = NULL;
 m_pRecentFileList = NULL;
 m_pDocManager = NULL;
 m_atomApp = m_atomSystemTopic = NULL; //微软懒鬼?或者他认为 
 //这样连等含义更明确?
 m_lpCmdLine = NULL;
 m_pCmdInfo = NULL;

 // initialize wait cursor state
 m_nWaitCursorCount = 0;
 m_hcurWaitCursorRestore = NULL;

 // initialize current printer state
 m_hDevMode = NULL;
 m_hDevNames = NULL;
 m_nNumPreviewPages = 0; // not specified (defaults to 1)

 // initialize DAO state
 m_lpfnDaoTerm = NULL; // will be set if AfxDaoInit called

 // other initialization
 m_bHelpMode = FALSE;
 m_nSafetyPoolSize = 512; // default size
}

  很显然,theApp成员变量都被赋予OS启动的这个当前线程相关的值,如代码:

m_hThread = ::GetCurrentThread();//theApp的线程句柄等于当前线程句柄 
m_nThreadID = ::GetCurrentThreadId();//theApp的线程ID等于当前线程ID

  所以CWinApp类几乎只是为MFC程序的第一个线程量身定制的,它不需要也不能被AfxBeginThread或theApp.CreateThread"再次"启动。这就是CWinApp类和theApp全局变量的内涵!如果你要再增加一个UI线程,不要继承类CWinApp,而应继承类CWinThread。而参考第1节,由于我们一般以主线程(在MFC程序里实际上就是OS启动的第一个线程)处理所有窗口的消息,所以我们几乎没有再启动UI线程的需求

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fairy_study/archive/2010/05/16/5596453.aspx

深入浅出Win32多线程设计之MFC的多线程-线程与消息队列(经典)的更多相关文章

  1. 图解MFC基本框架(深入消息映射机制)

    首先,先看整体的消息流向图: 上图解释: 起点是消息循环,在winmain函数中(mfc中winmain函数是隐含的调用的,在app全局对象构造完后紧接着调用winmain函数),while循环中不断 ...

  2. 深入浅出Win32多线程程序设计之基本概念

    一.深入浅出Win32多线程程序设计之基本概念[转] 引言 从单进程单线程到多进程多线程是操作系统发展的一种必然趋势,当年的DOS系统属于单任务操作系统,最优秀的程序员也只能通过驻留内存的方式实现所谓 ...

  3. MFC的多线程操作

    记得用MFC做了一个图像自动修复软件,当时没有多线程操作这一概念,由于图像修复算法比较复杂,因此,当执行图像修复时,程序就像卡死了似得而不能做其他操作.其实MFC对这种情况有一种很好地解决方案,那就是 ...

  4. MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决 转

    MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决   在多线程设计中,许多人为了省事,会将对话框类或其它类的指针传给工作线程,而在工作线程中调用该类的成员函数或成员变量等等. ...

  5. .NET应用架构设计—服务端开发多线程使用小结(多线程使用常识)

    有一段时间没有更新博客了,最近半年都在着写书<.NET框架设计—大型企业级框架设计艺术>,很高兴这本书将于今年的10月份由图灵出版社出版,有关本书的具体介绍等书要出版的时候我在另写一篇文行 ...

  6. 转:MFC创建多线程实例

    作者:http://blog.csdn.net/wangningyu/article/details/4404134 平时在MFC里使用多线程时其实是很方面的,因为微软提供了一个API让我们很方面的去 ...

  7. 第二单元总结:基于synchronize锁的简单多线程设计

    单元统一的多线程设计策略 类的设计 电梯 每部电梯为一个线程. 电梯从调度器接收原子指令,知晓自己的状态(内部的人/服务的人.运行方向.所在楼层) 原子指令包括且仅包括: 向上走一层 / 向下走一层 ...

  8. c++ 网络编程(九)LINUX/windows-IOCP模型 多线程超详细教程及多线程实现服务端

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9661012.html 先讲Linux下(windows下在后面可以直接跳到后面看): 一.线程 ...

  9. 多线程的基本概念和Delphi线程对象Tthread介绍

    多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru    WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...

随机推荐

  1. Spring Boot Memory Performance

    The Performance Zone is brought to you in partnership with New Relic. Quickly learn how to use Docke ...

  2. HDU 2962 Trucking

    题目大意:给定无向图,每一条路上都有限重,求能到达目的地的最大限重,同时算出其最短路. 题解:由于有限重,所以二分检索,将二分的值代入最短路中,不断保存和更新即可. #include <cstd ...

  3. 前端笔试面试中的常用知识点总结(CSS)

    1.CSS选择器的优先级!important  > 内联 > id选择器 > 类选择器 > 标签选择器多个类选择器叠加(256)之后的优先级大于一个id选择器!importan ...

  4. 在ASP.NET中动态加载内容(用户控件和模板)

    在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...

  5. JAVA知识的相关积累--用于自己以后查找

    基础: Properties类操作文件,主要是对配置文件的操作.java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容 ...

  6. mysql时间与字符串相互转换

    时间.字符串.时间戳之间的互相转换很常用,但是几乎每次使用时候都喜欢去搜索一下用法:本文整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转date.字符串转时间戳.时间戳转da ...

  7. windows cmd: 打开windows系统程序或服务的常见命令

    Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...

  8. Oracle Hint用法总结

    1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量,使资源消耗最小化. 例如: SELECT /*+ALL+_ROWS*/ EMP_NO,EMP_NAM,DAT_I ...

  9. JDBC_获取数据库连接

    <span style="font-size:24px;">package src.com.jdbc.java; import java.io.IOException; ...

  10. [LeetCode]题解(python):018-4Sum

    题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...