我写了一个线程类如下 希望能够把任意类的成员函数 作为我的线程函数  下面为网上找的资料template <typename T>
struct ThreadInfo
{
typedef DWORD (T::*THREADFUNC1)(LONG);
T* m_obj;
LONG m_param;
THREADFUNC1 m_lpthreadFunc; 
};class NULLCLASS
{
};typedef ThreadInfo<NULLCLASS> ThInfo;
typedef ThreadInfo<NULLCLASS>::THREADFUNC1 THREADFUNC;
 
class CMyThread
{
public:
CMyThread(void);
~CMyThread(void);
template<typename tname>
BOOL Start(tname* obj,THREADFUNC func,LONG param = 0)
{  
if(m_handle || obj == NULL || func == NULL)
return FALSE; //已有线程运行 ThreadInfo<tname>* lpv = new ThreadInfo<tname>;
lpv->m_obj = obj;
lpv->m_param = param;
lpv->m_lpthreadFunc =  static_cast((ThreadInfo<tname>::THREADFUNC1))func; m_info = (ThInfo*)lpv;
DWORD gdwThreadId = 0;
m_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,m_info,0,&gdwThreadId);
return (BOOL)m_handle;
}
private:
static DWORD WINAPI ThreadProc(ThInfo* obj);private:
HANDLE m_handle;
ThInfo* m_info;};
下面是我写的MFC 类// CInstinstanceCollectDlg 对话框
class CInstinstanceCollectDlg : public CDialog
{
DECLARE_EASYSIZE
// 构造
public:
CInstinstanceCollectDlg(CWnd* pParent = NULL); // 标准构造函数// 对话框数据
enum { IDD = IDD_INSTINSTANCECOLLECT_DIALOG }; protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint(); DECLARE_MESSAGE_MAP()
public:
// 主显示收集分类界面
。。



protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
public:protected:
。。
public:

DWORD  CollectWaste(LONG lParam);
public: CMyThread m_myThread;};
 下面是 。cpp  文件的东西
BOOL CInstinstanceCollectDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
m_myThread.Start(this,(THREADFUNC)CollectWaste);   //就是这里报错 
return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}
但是在调用
m_myThread.Start(this,(THREADFUNC)CollectWaste);   //编译报错
Error 1 error C3867: 'CInstinstanceCollectDlg::CollectWaste': function call missing argument list; use '&CInstinstanceCollectDlg::CollectWaste' to create a pointer to member e:\my project\instinstancecollect\instinstancecollect\instinstancecollectdlg.cpp 803

解决方案 »

  1.   

    提示已经说了解决方法
    m_myThread.Start(this,(THREADFUNC)&CInstinstanceCollectDlg::CollectWaste);
      

  2.   

    如果成员函数作为线程函数,则必须是静态的
    因为非静态成员函数默认包含一个this指针
      

  3.   

    提示已经说了解决方法
    m_myThread.Start(this,(THREADFUNC)&CInstinstanceCollectDlg::CollectWaste);
    不好意思 这样的确不报错了 但是  
    DWORD  CInstinstanceCollectDlg::CollectWaste(void * lParam)
    {
    while(1)
    {
    int i=0;
    i++;
    }
    MessageBox(_T(" hhhhhhhhhhhhhh"));
    return 0;
    }
    在我的线程函数  增加断点时候 好像没有进去呢
      

  4.   

    while(1)
    {
    int i=0;
    i++;
    }
    这不是死循环么
      

  5.   

    Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process.创建成功了,不一定代表函数执行了
      

  6.   

    还是自己动手丰衣足食 。。问题出在 if(m_handle || obj == NULL || func == NULL)
     return FALSE; //已有线程运行 m_handle 刚开始时候他为NULL 所以根本没有创建  线程