给你一个封装好的 class, 转到 c++builder 下很容易
#pragma once #include "Delegate.h"
class CExThread { CWinThread* m_pThread; BOOL m_bThreadAlive; BOOL m_bStopThread; static UINT Start(LPVOID pParam);
public: CDelegate m_Delegate;
CExThread(void); ~CExThread(void);
bool Run(); void Stop(); };
#include "stdafx.h" #include "exthread.h"
CExThread::CExThread(void) { m_pThread = NULL; m_bThreadAlive = FALSE; m_bStopThread = FALSE; }
CExThread::~CExThread(void) { Stop(); }
bool CExThread::Run() { m_pThread = ::AfxBeginThread(Start, this); m_bThreadAlive = TRUE; return m_pThread != NULL; }
void CExThread::Stop() { if (m_pThread == NULL) return;
do { m_bStopThread = TRUE; } while (m_bThreadAlive); }
UINT CExThread::Start(LPVOID pParam) { CExThread* me = (CExThread*) pParam; me->m_bThreadAlive = TRUE; me->m_Delegate.Invoke(NULL, 0); me->m_bThreadAlive = FALSE; return 0; }
DELEGATE.H
#if !defined(_Class_Delegate_) #define _Class_Delegate_ #pragma once
/************************************************************************************************* Warning: Unsafe Code !!! *************************************************************************************************/
#define HANDLER(function) reinterpret_cast<PTR_CALLBACK>(function) #define SetEventHandler(instance, function) _SetEventHandler((instance), HANDLER(function))
class CDelegate; typedef void (CDelegate::*PTR_CALLBACK)(void* sender, DWORD para);
class CDelegate { PTR_CALLBACK m_pCallBack; CDelegate* m_pInstance;
public: CDelegate() { m_pCallBack = NULL; m_pInstance = NULL; }
void _SetEventHandler(void* pInstance, PTR_CALLBACK pCallBack) { m_pInstance = (CDelegate*) pInstance; m_pCallBack = pCallBack; }
void Invoke(void* sender, DWORD para) { if (m_pInstance != NULL && m_pCallBack != NULL) (m_pInstance->*m_pCallBack)(sender, para); } };
#endif |