在MFC中,有个for循环,想要在每一次的循环中创建一个线程,线程函数都是threadFun该怎么创建?请各位大虾帮忙◎^◎
解决方案 »
- MFC应用程序快捷键冲突
- 怎样得到Combo Box里面用户输入的值啊?
- 关于vc生成dll
- 又是一个CImageList的图片颜色问题[16位色、过滤色没有效果]
- 谁知道NTdhcp.exe是什么程序?干什么的?
- ocx最基本的问题,麻烦各位帮帮忙?
- 项目招标:人民币¥XXXX元 把VC的DLL转成COM组件。急!
- 哪位大瞎有功能强大的局域网聊天程序??? 给分多多
- 请教高手
- link error ,大家帮忙看看!!!!!!!!!
- Win32 API问题:怎样让同一个窗口消息处理函数区分不同窗口对象
- 请问SCSI_PASS_THROUGH和SCSI_PASS_THROUGH_DIRECT有什么区别呀?
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;
}
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);
}
hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
这样能通过编译
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);
}}
char sDir[256];
char tDir[256];
char dllLocation[256];
char isDeleteSFile[256];
};试试
不可以啊,因为是传递过来的参数,要求就是CString类型的
phThread = (CWinThread **)new CWinThread*[number];
for(int t=0;t<number;t++)
{
//threadinfo是定义的一个结构体变量
//参数已经设置
phThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
}
{
//threadinfo是定义的一个结构体变量
//参数已经设置
hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
}
我想知道这是什么意思?
本来是想要创建一个存放CWinThread指针类型的数组的,
正确的方法是wangli820给出的,CWinThread **phThread;
phThread = (CWinThread **)new CWinThread*[number];但是这样的话,虽然能够存放想要的指针了,但是还是出错
//参数已经设置
hThread[t] = AfxBeginThread(ThreadFunc,&threadinfo);
、
保证你的threadinfo变量是全局的,否则,,,,哈哈
我可以继续加分啊
函数呢?
信息给全点行不?报错调试下 定位错误在哪行吗?哎 真不愿回答了
1。threadinfo是临时变量,线程运行时这个结构早不存在了,你不得用&threadinfo这样取一个临时变量的地址并传给线程
2. 你在多个线程中共享了同一个threadinfo,这也是不行的!否则不同的线程之间要传入的数据不一样怎么办?对于每个线程,new 一个threadinfo传进去,而不要犯上面的错误
不好意思,是我的错,全部的代码在下面列出:
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;
}
你指的是我在创建的线程中使用了AfxMessageBox函数吗?
hisn = LoadLibrary(dllLocat); 这句也可以写在线程外面..
只需要载入一次.
最后释放掉.另外线程里可以写个for循环..调用次数可以随便设还有一个问题:
//threadinfo是定义的一个结构体变量
你这个变量在for循环里总是传同一个..所以线程函数的作用也是一样的
再一个线程函数用了锁进行了同步.
说白了..和你在一个线程里
放一个for.
然后调用那个函数..这样这函数也是同步执行的.连锁都不用