線程類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)); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM