.h文件创建了
public:
void ThreadFunc();protected:
HANDLE hThread;
DWORD ThreadID;.cpp创建文件
volatile BOOL m_bRun; void ThreadFunc()
{
CTime time;
CString strTime;
m_bRun=TRUE;
while(m_bRun)
{
time=CTime::GetCurrentTime();
strTime=time.Format("%H:%M:%S");
::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,strTime);
Sleep(1000);
}
}void CSingleThreadDlg::OnStart()   //一个按钮事件
{
// TODO: Add your control notification handler code here
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,NULL,0,&ThreadID);  //这个地方出错了 GetDlgItem(IDC_START)->EnableWindow(FALSE);
WaitForSingleObject(hThread,INFINITE);
GetDlgItem(IDC_START)->EnableWindow(TRUE);}提示出错:
D:\代码测试\SingleThread\SingleThreadDlg.cpp(207) : error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)'
        None of the functions with this name in scope match the target type
执行 cl.exe 时出错.SingleThread.exe - 1 error(s), 0 warning(s)安装书上写的,为什么是这样,改如何改呢?

解决方案 »

  1.   

    看的什么书?这不是误人子弟么?那个线程函数定义的根本不对。你还是直接看看msdn把
      

  2.   

    DWORD WINAPI ThreadFunc(void* thParam);
      

  3.   

    哦,忘记了
    static DWORD WINAPI ThreadFunc(void* thParam);
      

  4.   

    定义在.cpp文件中吧。void ThreadFunc()
    {
    CTime time;
    CString strTime;
    m_bRun=TRUE;
    while(m_bRun)
    {
    time=CTime::GetCurrentTime();
    strTime=time.Format("%H:%M:%S");
    ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,strTime);
    Sleep(1000);
    }
    return 0;}如要一个返回值,加入我红色字的地方就可以吧。
      

  5.   

    在.h:
    类外部声明线程  void ThreadFunc();
    在类内部public声明 一个处理函数 void ShowTime();在.cpp:
    CWnd* g_pWnd; //声明全局CWnd指针 
    在OnInitDialog中
    g_pWndTsf = this;      //窗口类指针传给CWnd
    //定义ShowTime()
    void CSingleThreadDlg::ShowTime()
    {
        CTime time;
        CString strTime;
        m_bRun=TRUE;
        while(m_bRun)
        {
            time=CTime::GetCurrentTime();
            strTime=time.Format("%H:%M:%S");
            ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,strTime);
            Sleep(1000);
        }
    }
    //定义线程
    void ThreadFunc()
    {
    ((CSingleThreadDlg*)g_pWndTsf)->ShowTime(); //此处调用处理函数
    }线程声明在类内部的暂时还没研究