所以我正在鑽研MFC的世界,特別是定製使用CWinThread來實現工作線程。我已經以其他方式成功實現了工作線程,因此使用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);
};
的implemetation
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實現接收消息?
感謝
,所以如果我想有螺紋isnever這個工作者線程坐在一個循環中工作,它不可能接收消息? – tuskcode 2015-02-10 13:09:25
或者我可以調用基類Run後進入我的覆蓋運行,然後繼續工作在循環? – tuskcode 2015-02-10 13:13:39
你可以在OnIdle中做你的工作......(正如我已經寫過的),或者你可以不時地使用PeekMesage/AfxPumpMessage來檢查是否有消息在等待。 – xMRi 2015-02-10 13:23:05