一、概要
CWinThread类封装了对线程的操作,一个CWinThread对象代表在应用程序中一个线程的执行。在MFC应用程序中,主执行线程是CWinThread派生类CWinApp的派生类对象。由CWinApp类派生的新类都是用户界面线程。
二、类图位置
三、多线程的实现方法
四、CWinThread头文件
class CWinThread : public CCmdTarget
{
DECLARE_DYNAMIC(CWinThread)
friend BOOL AfxInternalPreTranslateMessage(MSG* pMsg);
public:
// Constructors
CWinThread();
/// 开始一个CWinThread对象的执行
BOOL CreateThread(DWORD dwCreateFlags = 0, UINT nStackSize = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
///常用的启动线程的方法
///※① CreateThread()
///※② _beginThread _beginThreadEx()
///※③ AfxBeginThread() MFC全局函数
// Attributes
///保存指向应用程序的主窗口的指针
CWnd* m_pMainWnd; // main window (usually same AfxGetApp()->m_pMainWnd)
///指向容器应用程序的主窗口,当一个OLE服务器被现场激活时
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;
/// 当前线程的ID
DWORD m_nThreadID; // this thread's ID
/// 获取当前线程的优先级
int GetThreadPriority();
/// 设置当前线程的优先级
BOOL SetThreadPriority(int nPriority);
// Operations
/// 增加一个线程的挂起计数
/// 当线程的挂起计数大于0时,该线程将暂停执行,称之为挂起状态。
DWORD SuspendThread();
///减少一个线程的挂起计数
/// 当线程的挂起计数减少到0时,恢复线程的执行。
DWORD ResumeThread();
/// 向另外的CWinThread对象传递一条消息
BOOL PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam);
// Overridables
// thread initialization
///重载以实现线程实例的初始化
virtual BOOL InitInstance();
// running and idle processing
///线程的具有消息收发功能的控制函数,可重载以定制缺省的消息循环
virtual int Run();
///在消息被发送到Windows函数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
// thread termination
/// 重载以进行线程终止时的清理工作
virtual int ExitInstance(); // default will 'delete this'
// Advanced: exception handling
///截获线程消息和命令处理函数出现的所有未处理的异常
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;
#endif
void CommonConstruct();
virtual void Delete();
// 'delete this' only if m_bAutoDelete == TRUE
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:
BOOL DispatchThreadMessageEx(MSG* msg); // helper
void DispatchThreadMessage(MSG* msg); // obsolete
};