CWinThread 消息映射
CWinThread message maps
所以我正在研究 MFC 的世界,特别是自定义使用 CWinThread 来实现工作线程。我已经通过其他方式成功实现了工作线程,因此将 CWinThread 用于工作线程的主要动机是利用消息映射。
为了将来的使用,我将 CWinThread 派生到最终将成为某种形式的超级 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 的基础 class 实现 (__super::运行),您将永远不会收到消息!
不要使用 isdone_。而是使用 PostQuitMessage 来终止当前的工作线程。只需使用 运行 运行 线程的基本实现并发送消息。
您还可以使用 OnIdle 或 CWinThread 的其他功能,做一些工作...
只需调用 Sleep 就会阻塞您的线程,但线程永远不会被 windows 消息中断
所以我正在研究 MFC 的世界,特别是自定义使用 CWinThread 来实现工作线程。我已经通过其他方式成功实现了工作线程,因此将 CWinThread 用于工作线程的主要动机是利用消息映射。
为了将来的使用,我将 CWinThread 派生到最终将成为某种形式的超级 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 的基础 class 实现 (__super::运行),您将永远不会收到消息!
不要使用 isdone_。而是使用 PostQuitMessage 来终止当前的工作线程。只需使用 运行 运行 线程的基本实现并发送消息。
您还可以使用 OnIdle 或 CWinThread 的其他功能,做一些工作...
只需调用 Sleep 就会阻塞您的线程,但线程永远不会被 windows 消息中断