如何将Cwnd Class的句柄传递给它的调用线程?

时间:2013-11-21 06:44:11

标签: c++ visual-studio-2010 mfc

我有2个类,它们是从CWnd类继承的CPacketDispatcher和从CWinThread开始的CPacketAnalyzer。 CPacketDispatcher创建了几个CPacketAnalyzer线程和 当分析器线程完成其工作时, 线程将通过调用SendMessage或PostMessage函数将其状态通知给Dispatcher。 但是要在线程中调用SendMessage函数,据我所知,我应该得到PacketDispatcher句柄。 因此,当线程在调度程序中恢复时,我试图将其移交,但我认为它不起作用。 没有compliation错误,但CPacket Analyzer从未将消息传递给SendMessage函数中的调度程序类。

请参阅下面的省略代码,给我你的专业帮助!!

// PacketDispatcher.h

class CPacketDispatcher : public CWnd
{
    DECLARE_DYNAMIC(CPacketDispatcher)

    public:
    CString                             CLASS_TAG;
    CLogger*                            wLog;

    static CPacketDispatcher*   packetDispatcher;
    static CMutex                       m_Mutex;

private:
    int*                        m_threadId;    
    int*                        m_threadState; 

    int                     m_taskThreadId; 
    CPacketAnalyzerThread*              m_packetAnalyzerThread[MAX_THREAD_COUNT]; 

public:
    CPacketDispatcher();
    virtual ~CPacketDispatcher();

protected:
    DECLARE_MESSAGE_MAP()
public:
    static CPacketDispatcher* getInstance(void);
    int run(CPacketDEF *packet);
    void initializeWorkingThread(void);
    int findIdleThread(void);
protected:
    afx_msg LRESULT OnUpdatethreadstate(WPARAM wParam, LPARAM lParam);
};

// PacketDispatcher.cpp

int CPacketDispatcher::run(CPacketDEF *packet)
{

    m_taskThreadId=findIdleThread();
    m_packetAnalyzerThread[m_taskThreadId]->m_pRcvPacket = packet;
    m_packetAnalyzerThread[m_taskThreadId]->m_pParentHwnd = this->m_hWnd;
    m_packetAnalyzerThread[m_taskThreadId]->ResumeThread();
    wLog->setLogTextOnDlg(CLASS_TAG,FUNC_TAG,"Exit Point"); 
    return 0;
}

// PacketAnalyzerThread.h

class CPacketAnalyzerThread : public CWinThread
{
    DECLARE_DYNCREATE(CPacketAnalyzerThread)

//definition
public:
    CString CLASS_TAG;
    CLogger* wLog;

    int     m_threadId;
    HWND    m_pParentHwnd;

//attribute
public:
    CPacketDEF *m_pRcvPacket;

protected:
    CPacketAnalyzerThread();           
    virtual ~CPacketAnalyzerThread();

public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();

protected:
    DECLARE_MESSAGE_MAP()
public:
    virtual int Run();


    int executePacketParsing(void);
    void enqueuePacket(void);
};

// PacketAnalyzerThread.cpp

int CPacketAnalyzerThread::Run()
{

    while(true){    

        executePacketParsing();
        SendMessage(m_pParentHwnd,WM_UpdateThreadState,(WPARAM) this->m_threadId,(LPARAM) THREAD_IDLE);
        TRACE0("After SEND MESSAGE");   
        this->SuspendThread(); // after processing one packet, the thread will be suspended until packetDispatcher resumes the thread. 
        SendMessage(m_pParentHwnd,WM_UpdateThreadState,(WPARAM) this->m_threadId,(LPARAM) THREAD_BUSY);
    }

    return CWinThread::Run();
}

0 个答案:

没有答案