如我的這樣一個程式,我想傳遞一個參數給這個線程,如1,2,3,4,5...依次傳遞給那多個線程,我這個數該如何傳遞進去呢?創建線程時我已經傳遞了一個this指針,因為我要用到對話框類中的變量,所以必須傳遞一個this指針,但同時我還想傳遞諸如
1,2,3,4,5等int型變量如何實現呢?
void CDownMp3Dlg::OnFilecopy()
{
DWORD dwDownThrdId;
HANDLE hDownThrd;
for(int i=0;i<5;i++)
{
hDownThrd=CreateThread(NULL,0,DownLoadThread,this,0,&dwDownThrdId);
if(hDownThrd==NULL)
{
AfxMessageBox("can't create the download thrd!");
return;
}
CloseHandle(hDownThrd);
}
}DWORD WINAPI DownLoadThread(LPVOID n)
{
CDownMp3Dlg *dlg=(CDownMp3Dlg*)n;
dlg->GetDlgItem(IDC_FILECOPY)->EnableWindow(FALSE);
dlg->m_DownProgress.SetPos(0);
dlg->m_DownProgress.SetRange(0,100);
dlg->m_DownProgress.SetStep(20);
...
}
1,2,3,4,5等int型變量如何實現呢?
void CDownMp3Dlg::OnFilecopy()
{
DWORD dwDownThrdId;
HANDLE hDownThrd;
for(int i=0;i<5;i++)
{
hDownThrd=CreateThread(NULL,0,DownLoadThread,this,0,&dwDownThrdId);
if(hDownThrd==NULL)
{
AfxMessageBox("can't create the download thrd!");
return;
}
CloseHandle(hDownThrd);
}
}DWORD WINAPI DownLoadThread(LPVOID n)
{
CDownMp3Dlg *dlg=(CDownMp3Dlg*)n;
dlg->GetDlgItem(IDC_FILECOPY)->EnableWindow(FALSE);
dlg->m_DownProgress.SetPos(0);
dlg->m_DownProgress.SetRange(0,100);
dlg->m_DownProgress.SetStep(20);
...
}
然后再定义一个结构体,把对话框的this指针和你要传递的数组做为结构体的成员变量
如
int array[] = {1,2,3,4,5...}
struct parm
{
array,
this
};
在传递参数的时候把这个结构体的指针传递过去!希望对你有所帮助!
這樣不行,編譯總是不過.
typedef struct
{
LPVOID *p;
int array[5];
}ThrdParam;
ThrdParam thdParam; hDownThrd=CreateThread(NULL,0,DownLoadThread,thdParam,0,&dwDownThrdId);
if(hDownThrd==NULL)
{
AfxMessageBox("can't create the download thrd!");
return;
}
CloseHandle(hDownThrd);E:\MyDebugProg\DownFile\DownMp3Thread\DownMp3Dlg.cpp(436) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (ThrdParam)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
E:\MyDebugProg\DownFile\DownMp3Thread\DownMp3Dlg.cpp(447) : error C2440: 'type cast' : cannot convert from 'ThrdParam' to 'class CDownMp3Dlg *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.DownMp3.exe - 2 error(s), 0 warning(s)
map<string,string> _filelist;
CString _ftpuser;
CString _ftppws;
bool* _stopflag ;
} ARGLIST ;//线程参数结构
ARGLIST _argument ;//线程参数 _argument._filelist=_filelist ;
_argument._ftpuser=user;
_argument._ftppws=pass;
_argument._stopflag=&_stopflag; //给线程参数赋值
AfxBeginThread(updateVer, (void*)(&_argument)); //开始线程
{
LPVOID p;
int array[5];
}ThrdParam;void CDownMp3Dlg::OnFilecopy()
{
DWORD dwDownThrdId;
HANDLE hDownThrd;
ThrdParam thdParam;
thdParam.p=this;/* ARGLIST _argument ;//?程??
_argument._filelist=_filelist ;
_argument._ftpuser=user;
_argument._ftppws=pass;
_argument._stopflag=&_stopflag; //??程???值
AfxBeginThread(updateVer, (void*)(&_argument)); //?始?程
*/ hDownThrd=CreateThread(NULL,0,DownLoadThread,(void*)(&thdParam),0,&dwDownThrdId);
if(hDownThrd==NULL)
{
AfxMessageBox("can't create the download thrd!");
return;
}
CloseHandle(hDownThrd);
}出錯:
Compiling...
DownMp3Dlg.cpp
E:\MyDebugProg\DownFile\DownMp3Thread\DownMp3Dlg.cpp(452) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (ThrdParam)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
E:\MyDebugProg\DownFile\DownMp3Thread\DownMp3Dlg.cpp(463) : error C2440: 'type cast' : cannot convert from 'ThrdParam' to 'class CDownMp3Dlg *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.DownMp3.exe - 2 error(s), 0 warning(s)
typedef struct
{
LPVOID p;
int array[5];
}ThrdParam;void CDownMp3Dlg::OnFilecopy()
{
DWORD dwDownThrdId;
HANDLE hDownThrd;
ThrdParam* pParam = NULL;
for(int i=0;i<5;i++)
{
pParam = new ThrdParam;
pParam->p = this;
hDownThrd=CreateThread(NULL,0,DownLoadThread,(void*)pParam,0,&dwDownThrdId);
if(hDownThrd==NULL)
{
AfxMessageBox("can't create the download thrd!");
return;
}
CloseHandle(hDownThrd);
}
}DWORD WINAPI DownLoadThread(LPVOID n)
{
ThrdParam* pParam = (ThrdParam*)n;
....
if (pParam != NULL)
{
delete pParam;
}
}
CreateThread(NULL,0,DownLoadThread,(void*)(&thdParam),0,&dwDownThrdId);
添称
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DownLoadThread,(void*)(&thdParam),0,&dwDownThrdId);
因改就是可以了!