Not sure what is wrong with parameter 3 or the setup?
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
—
void CNumbergeneratorDlg::OnBtn3()
{
//CreateThread
hThread1 = CreateThread(NULL, 0, Thread1, this, 0, NULL);//<--is "this" correct
WaitForSingleObject(hThread1,INFINITE);
TerminateThread(hThread1,0);
CloseHandle(hThread1);
}
DWORD WINAPI CNumbergeneratorDlg::Thread1(LPVOID iValue)
{
CreateNumber();
return 0;
}
??? casting for “this”
DWORD WINAPI CNumbergeneratorDlg::Thread1(LPVOID iValue)
{
(CDialog)iValue->CreateNumber();
return 0;
}
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
This is what I did with your guys input… thanks
void CNumbergeneratorDlg::OnBtn3()
{
//CreateThread
CNumbergeneratorDlg *pp = this;
hThread1 = CreateThread(NULL, 0, Thread1, pp, 0, NULL);
// WaitForSingleObject(hThread1,INFINITE);
// TerminateThread(hThread1,0);
// CloseHandle(hThread1);
}
DWORD WINAPI CNumbergeneratorDlg::Thread1(LPVOID iValue)
{
CNumbergeneratorDlg *pp = (CNumbergeneratorDlg*)iValue;
pp->CreateNumber();
return 0;
}
void CNumbergeneratorDlg::CreateNumber()
{
long m;
j = 0;
for(long i = 0; i < 1000;i++){
m = 0;
for(long k = 0; k < 1000000;k++){
m ++;
}
j++;
}
AfxMessageBox("Done count");
TerminateThread(hThread1,0);
CloseHandle(hThread1);
}
void CNumbergeneratorDlg::OnBtn4()
{
TerminateThread(hThread1,0);
CloseHandle(hThread1);
CString c;
c.Format("%d", j);
MessageBox(c);
}
Did you declare the function
Thread1as astaticmember function inCNumbergeneratorDlg? If not, please do so.Non-static class functions have an implicit additional parameter for
this. So, type casting will not work. On the other hand, static methods do not carrythisparameter.Then, you might wonder how you can access class members inside
Thread1. A typical technique is passingthisviaiValue.