多线程编程之二——MFC中的多线程开发
2007-03-17 22:00:08 来源:WEB开发网在UIThread.h中添加#include "UIThreadDlg.h"
并在CUIThread类中添加protected变量CUIThread m_dlg:class CUIThread : public CWinThread
{
DECLARE_DYNCREATE(CUIThread)
protected:
CUIThread(); // protected constructor used by dynamic creation
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUIThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
protected:
CUIThreadDlg m_dlg;
virtual ~CUIThread();
// Generated message map functions
//{{AFX_MSG(CUIThread)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
分别重载InitInstance()函数和ExitInstance()函数:BOOL CUIThread::InitInstance()
{
m_dlg.Create(IDD_UITHREADDLG);
m_dlg.ShowWindow(SW_SHOW);
m_pMainWnd=&m_dlg;
return TRUE;
}
int CUIThread::ExitInstance()
{
m_dlg.DestroyWindow();
return CWinThread::ExitInstance();
}
双击按钮IDC_UI_THREAD,添加消息响应函数:void CMultiThread6Dlg::OnUiThread()
并在MultiThread6Dlg.cpp的开头添加:
{
CWinThread *pThread=AfxBeginThread(RUNTIME_CLASS(CUIThread));
}#include "UIThread.h"
好了,编译并运行程序吧。每单击一次“用户界面线程”按钮,都会弹出一个线程对话框,在任何一个线程对话框内按下鼠标左键,都会弹出一个消息框。
(未完待续)
更多精彩
赞助商链接