CThread类

class IEventHandle 
{
public :
    virtual void callback(DWORD ret)=0;
};

class CThread 
{
public:
 CThread();
 virtual ~CThread();
public:
 DWORD CALLBACK Thread();
    bool    start();
    void    setEventHandle(IEventHandle *p) {m_pCallback=p;}
    virtual DWORD execute()=0;
public:
    IEventHandle * m_pCallback;
};

#endif

 

#include "stdafx.h"
#include "Thread.h"
#include <process.h>

CThread::CThread()
{  
    m_pCallback=NULL;
}

CThread::~CThread()
{

}

DWORD CALLBACK CThread::Thread()
{
 DWORD ret=-1;
    try
    {  
        ret=execute();
    }
    catch(...)
    {
        ret=-99;
    }

    try
    {
        if (m_pCallback)
            m_pCallback->callback(ret);
    }
    catch(...){}

    return 0;

}

bool CThread::start()
{
 union
 {
  DWORD (CALLBACK CThread::*Member)();
  UINT (CALLBACK *Static)(LPVOID);
 } ThreadFunction;

 ThreadFunction.Member = &CThread::Thread;

 UINT id;
 HANDLE hThread = (HANDLE)
  _beginthreadex(NULL, 0, ThreadFunction.Static, this, 0, &id);
 if (hThread == NULL)
 {
  return false;
 }
 CloseHandle(hThread);
 return true;
}

展开阅读全文

没有更多推荐了,返回首页