线程类CWinThread

CWinThread类是MFC用来封装线程的

class CWinThread : public CCmdTarget
{
    DECLARE_DYNAMIC(CWinThread)

    friend BOOL AfxInternalPreTranslateMessage(MSG* pMsg);

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;     // 指向应用程序主窗口,当一个OLE服务器被现场激活时active main window (may not be m_pMainWnd)
    BOOL m_bAutoDelete;     // 线程结束时是否销毁对象

    // only valid while running
    HANDLE m_hThread;       // 线程句柄
    operator HANDLE() const;
    DWORD m_nThreadID;      // 线程ID

    int GetThreadPriority();
    BOOL SetThreadPriority(int nPriority);   //设置线程优先级

// Operations
    DWORD SuspendThread();       //暂停线程
    DWORD ResumeThread();       //回复线程
    BOOL PostThreadMessage(UINT message, WPARAM wParam, LPARAM lParam);  //向线程发送消息
  
// Overridables
    // thread initialization
    virtual BOOL InitInstance();

    // running and idle processing
    virtual int Run();
    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
};   

                                

 

CWinThread类的虚函数

                                            

 

if (!pThread->InitInstance())
    {
        if (pThread->m_pMainWnd != NULL)   //创建窗口
        {
            TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWnd\n");
            pThread->m_pMainWnd->DestroyWindow();
        }
        nReturnCode = pThread->ExitInstance();
        goto InitFailure;
    }
    nReturnCode = pThread->Run();  //消息循环
int CWinThread::Run()
{
for
(;;) { // phase1: check to see if we can do idle work while (bIdle && !::PeekMessage(&(pState->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)) if (IsIdleMessage(&(pState->m_msgCur))) { bIdle = TRUE; lIdleCount = 0; } } while (::PeekMessage(&(pState->m_msgCur), NULL, NULL, NULL, PM_NOREMOVE)); } }
BOOL CWinThread::PumpMessage()
{
  return AfxInternalPumpMessage();
}
BOOL AFXAPI AfxInternalPumpMessage()
{
if (!::GetMessage(&(pState->m_msgCur), NULL, NULL, NULL))

if (pState->m_msgCur.message != WM_KICKIDLE && !AfxPreTranslateMessage(&(pState->m_msgCur)))
    {
        ::TranslateMessage(&(pState->m_msgCur));
        ::DispatchMessage(&(pState->m_msgCur));
    }

}

 

posted @ 2020-03-29 23:22  坦坦荡荡  阅读(2203)  评论(0)    收藏  举报