首先可以 在 vc中 新建一个class ,是以 CWinThread为 基类
class CProducerThread : public CWinThread
{
DECLARE_DYNCREATE(CProducerThread)
public:
CProducerThread(AFX_THREADPROC pfnThreadproc,void *parent);
static UINT ThreadFunc(LPVOID param);
virtual void Go(); // 是补上的
virtual ~CProducerThread();
protected:
CProducerThread(); // protected constructor used by dynamic creation
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CProducerThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
public:
void *m_parent;
protected:
// Generated message map functions
//{{AFX_MSG(CProducerThread)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CProducerThread::CProducerThread(AFX_THREADPROC pfnThreadproc,void *parent):CWinThread(pfnThreadproc,NULL)
{
m_parent=parent;
m_bAutoDelete=FALSE;
m_pThreadParams=this;
}
UINT CProducerThread::ThreadFunc(LPVOID param)
{
CProducerThread *pThread=(CProducerThread *)param;
pThread->Go();
return 0;
}
void CProducerThread::Go()
{
CpvDlg *p=static_cast<CpvDlg *>(m_parent);
}
main 中:
CProducerThread *p=new CProducerThread(CProducerThread::ThreadFunc,this);
p->CreateThread();
析构函数中:WaitForSingleObject(p->m_hThread,INFINITE) delete p。。 如果上面的m_bAutoDelete=FALSE; ,m_bAutoDelete=TRUE,则不需要删除。