多线程专题之MFC创建线程(2)

现在,我们再写一下带有消息循环的线程

1 从CWinThread派生自己的类(CAfxThread) :

class CAfxThread : public CWinThread
{
 DECLARE_DYNCREATE(CAfxThread)
protected:
 CAfxThread();           // protected constructor used by dynamic creation

// Attributes
public:

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CAfxThread)
 public:
 virtual BOOL InitInstance();
 virtual int ExitInstance();
 //}}AFX_VIRTUAL

// Implementation
protected:
 virtual ~CAfxThread();

 // Generated message map functions
 //{{AFX_MSG(CAfxThread)
  // NOTE - the ClassWizard will add and remove member functions here.
 afx_msg void OnAfxTest1Msg(WPARAM wParam, LPARAM lParam);
 afx_msg void OnAfxTest2Msg(WPARAM wParam, LPARAM lParam);
 afx_msg void OnAfxHelloMsg(WPARAM wParam,LPARAM lParam);
 //}}AFX_MSG

 DECLARE_MESSAGE_MAP()
};

CAfxThread::CAfxThread()
{
}

CAfxThread::~CAfxThread()
{
}

BOOL CAfxThread::InitInstance()
{
 // TODO:  perform and per-thread initialization here
/*
//实现用户界面线程 
 CFrameWnd* pWnd = NULL;

 pWnd = new CFrameWnd;
 if ( pWnd )
 {
  pWnd->Create(NULL,"UI Thread Window");
  pWnd->ShowWindow(SW_SHOW);
  pWnd->UpdateWindow();
  m_pMainWnd = pWnd;
 }
 return TRUE;
*/
/* CMultiThreadDlg dlg;
 m_pMainWnd = &dlg;

 dlg.DoModal();
 
 return FALSE;*/
 return TRUE;
}

int CAfxThread::ExitInstance()
{
 // TODO:  perform any per-thread cleanup here
 AfxMessageBox( _T("Oh ! I Know It! Bye!") );
 return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CAfxThread, CWinThread)
 //{{AFX_MSG_MAP(CAfxThread)
  // NOTE - the ClassWizard will add and remove mapping macros here.
 ON_THREAD_MESSAGE(WM_AFX_TEST1_MSG,OnAfxTest1Msg)
 ON_THREAD_MESSAGE(WM_AFX_TEST2_MSG,OnAfxTest2Msg)
 ON_THREAD_MESSAGE(WM_AFX_HELLO_MSG,OnAfxHelloMsg)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAfxThread message handlers

void CAfxThread::OnAfxTest1Msg( WPARAM wParam, LPARAM lParam )
{
 MessageBox(NULL,_T("Hello World!"),_T("Test"),MB_OK);
}

void CAfxThread::OnAfxTest2Msg( WPARAM wParam, LPARAM lParam )
{
 MessageBox(NULL,_T("Hello World!"),_T("Test"),MB_OK);
}

void CAfxThread::OnAfxHelloMsg(WPARAM wParam,LPARAM lParam)
{
 AfxMessageBox( _T("Hello! I'm A Thread!") );
 ::PostThreadMessage( AfxGetApp()->m_nThreadID, WM_AFX_HELLO_MSG, m_nThreadID, 0 );
}

2 定义自定义消息宏:

#define WM_AFX_TEST1_MSG WM_USER + 100
#define WM_AFX_TEST2_MSG WM_USER + 101
#define WM_AFX_HELLO_MSG WM_USER + 102

3 下面看看如何创建线程吧:

#include "AfxThread.h"
void CMultiThreadDlg::OnBtnUsageFour()
{
 // TODO: Add your control notification handler code here
//无界面,但有消息循环的线程
 CWinThread *pThread = NULL;
 
 pThread = AfxBeginThread (
  RUNTIME_CLASS(CAfxThread),      //线程类
  THREAD_PRIORITY_NORMAL,       //线程优先级
  0,            //Windows系统一般线程栈大小为1 MB,创建线程的数目与物理内存和栈空间大小有关
  0,            //线程创建标志,如:CREATE_SUSPENDED
  NULL );           //系统安全描述,NULL
 
 if ( pThread )
 {
  pThread->PostThreadMessage( WM_AFX_TEST1_MSG, 0, 0 );
  Sleep(2000);         //如果去掉这个就会丢失消息。呵呵,不信,你试试!
  pThread->PostThreadMessage( WM_AFX_TEST2_MSG, 0, 0 );
  //::PostThreadMessage( pThread->m_nThreadID, WM_AFX_TEST2_MSG, 0, 0 );
  //这两种向线程发送消息的方法都可以
  /*---------------------------------------------------------------------------*/
  Call PostThreadMessage.
  If it fails, call the Sleep function and call PostThreadMessage again.
  Repeat until PostThreadMessage succeeds.
  /*---------------------------------------------------------------------------*/

  Sleep(2000);
  pThread->PostThreadMessage( WM_QUIT, 0, 0 );//退出线程
 }
}

void CMultiThreadDlg::OnBtnUsageFive()
{
 // TODO: Add your control notification handler code here
//有界面的线程,当然了,也有消息循环了哦
/*
 AfxBeginThread (
  RUNTIME_CLASS(CAfxThread),      //线程类
  THREAD_PRIORITY_NORMAL,       //线程优先级
  0,            //Windows系统一般线程栈大小为1 MB,创建线程的数目与物理内存和栈空间大小有关
  0,            //线程创建标志,如:CREATE_SUSPENDED
  NULL );           //系统安全描述,NULL
 //消息循环会随着窗口的关闭有自动退出,并清理自身对象
*/

/* 
 CAfxThread myThread;
 myThread.CreateThread();
 //这样是不行的...
*/
}

呵呵,是不是很爽啊.接下来还有更精彩的呢!呵呵

你可能感兴趣的:(thread,多线程,null,mfc,attributes,initialization)