那就用AfxBeginThread吧,这个函数用起来比较简单

解决方案 »

  1.   

    _beginthread  函数应为全局函数或类的静态函数。如://全局函数
    UINT ThreadProc1(LPVOID)
    {
      ....
    }class A 
    {
      static UINT ThreadProc2(LPVOID);//静态函数
    };
      

  2.   

    谢谢帮助,能稍微详细一点解释一下全局和静态两种方法么
    我试了下声明为静态,就是在对话框类的头文件里声明
    class CMydicomtestDlg : public CDialog
    {
    public:
           static UINT BeginServer(LPVOID *empty);
           static UINT Receive( LPVOID *pClientSocket);
    .....
    }
    然后在cpp文件里UINT BeginServer(LPVOID *empty)
    {...
    }
    UINT Receive( LPVOID *pClientSocket)
    {
    ...if(_beginthread(Receive,0,rec)== -1)...              ####
    }
    void CMydicomtestDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        
        _beginthread( BeginServer,0,NULL);              ******
        m_listbox.AddString("server start");
       }
    结果报错为cannot convert parameter 1 from 'unsigned int (__cdecl *)(void ** )' to 'void (__cdecl *)(void *)'
            This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    以上是对####行的,对******的报错为'_beginthread' : cannot convert parameter 1 from 'unsigned int (void ** )' to 'void (__cdecl *)(void *)'
            None of the functions with this name in scope match the target type
    为什么两个报错会不同呢,不太明白
      

  3.   

    建议你还是用AfxBeginThread吧。
      

  4.   

    谢谢,我用了AfxBeginThread( BeginServer, NULL, THREAD_PRIORITY_NORMAL,  0,  0,  NULL );
    但是还是报错none of the 2 overloads can convert parameter 1 from type 'unsigned int (void ** )'
    BeginServer声明为全局UINT BeginServer(lpoid)和对话筐类成员函数都不行
    很想解决这个问题,谢谢
      

  5.   

    该是下面这样的吧?
    class CMydicomtestDlg : public CDialog
    {
    public:
           static UINT BeginServer(LPVOID *empty);
           static UINT Receive( LPVOID *pClientSocket);
    .....
    }
    然后在cpp文件里UINT BeginServer(LPVOID *empty)     //
    {
    ...
          if(_beginthread(Receive,0,rec)== -1)...
    ...
    }
    UINT Receive( LPVOID *pClientSocket)
    {
    ...
    }
    void CMydicomtestDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        
        _beginthread( BeginServer,0,NULL);
        m_listbox.AddString("server start");
       }
      

  6.   

    UINT Receive( LPVOID *pClientSocket)
    {
    ...if(_beginthread(Receive,0,rec)== -1)...              ####
    }
    怎么可以在线程函数里作这种递归调用呢?如果调用函数不能很快结束的话,要不了多久栈就用光了。再说参数也不对。CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
      

  7.   

    谢谢各位,最后大概按static定义的函数,还是有点小问题,没有在线程里第归调用了,不过用了强制参数类型转换,才能顺利开线程