gpt4 book ai didi

c++ - CWinThread 消息映射

转载 作者:行者123 更新时间:2023-11-28 06:26:32 25 4
gpt4 key购买 nike

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

为了将来的使用,我将 CWinThread 派生到最终将成为某种形式的父类(super class)中。请参阅以下声明。

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 实现接收消息吗?

谢谢

最佳答案

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

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

您还可以使用 OnIdle 或 CWinThread 的其他功能,来做一些工作...

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

关于c++ - CWinThread 消息映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28430714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com