在MFC中,有个for循环,想要在每一次的循环中创建一个线程,线程函数都是threadFun该怎么创建?请各位大虾帮忙◎^◎

解决方案 »

  1.   

    struct threadInfo{
    CString sDir;
    CString tDir;
    CString dllLocation;
    CString isDeleteSFile;
    };
    //公共变量
    CMutex m_Mutex;CWinThread *hThread; for(int t=0;t<number;t++)
    {
      //threadinfo是定义的一个结构体变量
               //参数已经设置
      hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    }//调用DLL的线程
    UINT DllThreadFunc(LPVOID pParam)
    {
    CSingleLock slLock(&m_Mutex);
    slLock.Lock();
    typedef void (*CFilePROC)(CString srcDir,CString tagDir,CString isDelSrcFile); threadInfo* pInfo = (threadInfo*)pParam;
    CString sDir = pInfo->sDir;
    CString tDir = pInfo->tDir;
    CString isDelSrcFile = pInfo->isDeleteSFile;
    CString dllLocat = pInfo->dllLocation; HINSTANCE hisn;
    hisn = LoadLibrary(dllLocat);
    if(hisn != NULL)
    {
      CFilePROC hClassifyFiles =(CFilePROC)GetProcAddress(hisn,"ClassifyFiles");
      hClassifyFiles(sDir,tDir,isDelSrcFile);
      ::FreeLibrary(hisn);
    }
    else 
    {
      AfxMessageBox(_T("ERROR: CAN NOT LOAD DLL!"));
      exit(0);
    }

    slLock.Unlock();
    return 0;
    }
      

  2.   

    这些是调用DLL的代码
    HINSTANCE hisn;
    hisn = LoadLibrary(dllLocat);
    if(hisn != NULL)
    {
    CFilePROC hClassifyFiles =(CFilePROC)GetProcAddress(hisn,"ClassifyFiles");
    hClassifyFiles(sDir,tDir,isDelSrcFile);
    ::FreeLibrary(hisn);
    }
    else  
    {
    AfxMessageBox(_T("ERROR: CAN NOT LOAD DLL!"));
    exit(0);
    }
      

  3.   

    CWinThread *hThread;
    hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    这样能通过编译
      

  4.   

    笔误啊,不好意思
    for(int t=0;t<number;t++)
    {
    //threadinfo是定义的一个结构体变量
      //参数已经设置
    //hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    //改为
    for(int t=0;t<number;t++)
    {
    //threadinfo是定义的一个结构体变量
      //参数已经设置
    hThread = AfxBeginThread(ThreadFunc,&threadinfo);
    }}
      

  5.   

    估计是threadinfo结构体的问题,不要用CString,用struct threadInfo{
    char sDir[256];
    char tDir[256];
    char dllLocation[256];
    char isDeleteSFile[256];
    };试试
      

  6.   


    不可以啊,因为是传递过来的参数,要求就是CString类型的
      

  7.   

    CWinThread **phThread;
    phThread = (CWinThread **)new CWinThread*[number];
    for(int t=0;t<number;t++)
    {
    //threadinfo是定义的一个结构体变量
      //参数已经设置
    phThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    }
      

  8.   

    CWinThread *hThread;for(int t=0;t<number;t++)
    {
    //threadinfo是定义的一个结构体变量
      //参数已经设置
    hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    }
    我想知道这是什么意思?
      

  9.   


    本来是想要创建一个存放CWinThread指针类型的数组的,
    正确的方法是wangli820给出的,CWinThread **phThread;
    phThread = (CWinThread **)new CWinThread*[number];但是这样的话,虽然能够存放想要的指针了,但是还是出错
      

  10.   

    //threadinfo是定义的一个结构体变量
      //参数已经设置
    hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);

    保证你的threadinfo变量是全局的,否则,,,,哈哈
      

  11.   

    threadinfo变量是全局变量啊这个问题都困扰我以两天了,真是郁闷的要死了希望各位行行好,帮帮忙……
    我可以继续加分啊
      

  12.   

    hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    函数呢?
    信息给全点行不?报错调试下 定位错误在哪行吗?哎  真不愿回答了
      

  13.   

    两个问题
    1。threadinfo是临时变量,线程运行时这个结构早不存在了,你不得用&threadinfo这样取一个临时变量的地址并传给线程
    2. 你在多个线程中共享了同一个threadinfo,这也是不行的!否则不同的线程之间要传入的数据不一样怎么办?对于每个线程,new 一个threadinfo传进去,而不要犯上面的错误
      

  14.   

    你给的截屏暴露了另外一个严重的问题:你可能再多线程中使用了窗口。MFC窗口禁止跨线程使用,看看下面的分析http://blog.vckbase.com/arong/archive/2010/05/21/15578.html#42651
      

  15.   


    不好意思,是我的错,全部的代码在下面列出:
    struct threadInfo{
    CString sDir;
    CString tDir;
    CString dllLocation;
    CString isDeleteSFile;
    };//给线程传递参数的节点结构CMutex m_Mutex;//公共变量
    theadInfo threadinfo;CWinThread **hThread;//在调用线程的主线程中定义
    pThread = (CWinThread **)new CWinThread*[number];//number是一个已知的变量
    for(int t=0;t<number;t++)
    {
    //threadinfo是定义的一个结构体变量
      //参数已经设置
    //arong1234说我共享了threadinfo,可是threadinfo不是就应该定义成公共变量嘛?
    hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
    }//线程函数,功能是调用DLL
    UINT ThreadFunc(LPVOID pParam)
    {
    CSingleLock slLock(&m_Mutex);
    slLock.Lock();
    //定义DLL中的函数指针
    typedef void (*CFilePROC)(CString srcDir,CString tagDir,CString isDelSrcFile);
    //给变量赋值
    threadInfo* pInfo = (threadInfo*)pParam;
    CString sDir = pInfo->sDir;
    CString tDir = pInfo->tDir;
    CString isDelSrcFile = pInfo->isDeleteSFile;
    CString dllLocat = pInfo->dllLocation;
    //调用DLL
    HINSTANCE hisn;
    hisn = LoadLibrary(dllLocat);
    if(hisn != NULL)
    {
    CFilePROC hClassifyFiles =(CFilePROC)GetProcAddress(hisn,"ClassifyFiles");
    hClassifyFiles(sDir,tDir,isDelSrcFile);
    ::FreeLibrary(hisn);
    }
    else  
    {
    AfxMessageBox(_T("ERROR: CAN NOT LOAD DLL!"));
    exit(0);
    }slLock.Unlock();
    return 0;
    }
      

  16.   


    你指的是我在创建的线程中使用了AfxMessageBox函数吗?
      

  17.   

    你不用每次新建线程吧.
    hisn = LoadLibrary(dllLocat); 这句也可以写在线程外面..
    只需要载入一次.
    最后释放掉.另外线程里可以写个for循环..调用次数可以随便设还有一个问题:
    //threadinfo是定义的一个结构体变量
    你这个变量在for循环里总是传同一个..所以线程函数的作用也是一样的
    再一个线程函数用了锁进行了同步.
    说白了..和你在一个线程里
    放一个for.
    然后调用那个函数..这样这函数也是同步执行的.连锁都不用
      

  18.   

    应该不是,怀疑你在一个线程中使用了另外一个线程中创建的窗口,但是AfxMessageBox应该是安全的。先把我我28楼说的东西解决掉吧
      

  19.   

    这么简单的问题,loadlibrary()每个进程只能加载一次。