CWinThread消息映射

时间:2015-02-10 11:37:52

标签: c++ multithreading mfc

所以我正在深入研究MFC的世界,特别是定制使用CWinThread来实现工作线程。我已经成功地以其他方式实现了工作线程,因此将CWinThread用于工作线程的主要动机是使用消息映射。

为了将来的使用,我将CWinThread派生成最终将成为某种形式的超类。请参阅以下声明..

class WinThreadBase : public CWinThread
{
    DECLARE_DYNCREATE(WinThreadBase)

protected:
    WinThreadBase();           // protected constructor used by dynamic creation
    DECLARE_MESSAGE_MAP()

public:

     BOOL isdone_;
};

通过以下

进行扩展和实施

声明

class WinThreadImplementation :public WinThreadBase {
    DECLARE_DYNCREATE(WinThreadImplementation)

protected:
    WinThreadImplementation(); //Declare protected because of dynamic creation

public: 

//The following CWinThread methods have to be overriden to complete specfic work.

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

private:

    CDialog* owner_;
    BOOL isinit_;

public:
    virtual ~WinThreadImplementation();
    void SetOwner( CDialog* pOwner ) { owner_ = pOwner; }
    BOOL Isinit() const { return isinit_; }
    DECLARE_MESSAGE_MAP()

//Message handler declaration
    void OnMyThreadMessage( WPARAM wParam, LPARAM lParam );
    void OnQuit( WPARAM wParam, LPARAM lParam );

};

实施

IMPLEMENT_DYNCREATE( WinThreadImplementation, WinThreadBase)

WinThreadImplementation::WinThreadImplementation() {
    owner_ = FALSE;
    isinit_ = FALSE;
}


WinThreadImplementation::~WinThreadImplementation() {
    //........Do some stuff here... //
}

BOOL WinThreadImplementation::InitInstance() {
    //Do some initialisation here..
    return TRUE; //returning true allows thread start successfully
}

int WinThreadImplementation::Run() {
    isinit_ = TRUE;
    while( !isdone_ ) {
        //Do some work...
        //TRACE( "Hello from pat's derived CWinThread" );
        Sleep( 1000 ); //Give other threads a chance to run..
    }

    owner_->PostMessage( WM_QUIT, 0, 0 ); // Tell our parent thread that this thread has finished work                                                                                 
    return 0;
}

BEGIN_MESSAGE_MAP(WinThreadImplementation,CWinThread)
    //Map messages to handler method here...
    //CWinThread messages must be handles like this....
    ON_THREAD_MESSAGE(WM_MYTHREADMESSAGE,OnMyThreadMessage)
END_MESSAGE_MAP()

//Put message handlers here...`
void WinThreadImplementation::OnMyThreadMessage( WPARAM wParam, LPARAM lParam )       {
    TRACE( "Hello from my message handler\n\r" );
}

现在实际发布消息

mywinthreadimpl_ = (WinThreadImplementation*)
        AfxBeginThread( RUNTIME_CLASS( WinThreadImplementation ),     THREAD_PRIORITY_NORMAL,
        0, CREATE_SUSPENDED );
    mywinthreadimpl_->SetOwner( this );
    mywinthreadimpl_->ResumeThread();

    while( !mywinthreadimpl_->Isinit() ); //Make sure that the thread has initialised before attempting to post a message

    if( PostThreadMessage( mywinthreadimpl_->m_nThreadID,   WM_MYTHREADMESSAGE, NULL, NULL ) ) {
        TRACE( "message was sent correctly\n" );
    }

因此,结果是它编译,我的CWinThread派生工作并进入重写运行功能。但我不能为我的生活收到PostThreadMessage发布的消息。

我已阅读以下内容

http://support.microsoft.com/kb/142415?wa=wsignin1.0

并得出结论,由于我使用的是VS 2010,因此这不适用于我。

任何人都可以建议我可能错过了什么,这会阻止我的CWinThread实现接收消息吗?

由于

1 个答案:

答案 0 :(得分:1)

不使用AfxPumpMessage或调用CWinThread(__super :: Run)的基类实现,您将永远不会收到消息!

不要使用isdone_。而是使用PostQuitMessage来终止当前的workerthread。只需使用Run的基本实现来运行线程并抽取消息。

你也可以使用OnIdle或CWinThread的其他功能来做一些工作......

只是调用Sleep会阻塞你的线程,但线程不会被Windows消息打断