Hello:
Is it possible to get some samples of simple multithread project from you? It is easy for me to start with an example built in workspace already.
Thank you for your kind help!
Here's part of an SMTP client application I created. Once I connect to the server I start a thread and pass it the "this" pointer. The thread receives data and calls my send member function depending on what data it gets. This is more then you need but it shows you how to start the thread and pass in a pointer to the class you started it from which can be very handy.
int CSMTPClientDlg::OnConnect()
{
if(Sock1.SetUp())
return 1;
UpdateData(TRUE);
// open socket
if(Sock1.OpenSock(m_csIPaddress,m_iPortNumber))
return 1;
// update dialog
m_csSocketStatus.Format("Opened port %i",m_iPortNumber);
UpdateData(FALSE);
// connect to server
if(Sock1.Connect(&addrRemote))
return 1;
m_ctrlData.EnableWindow(TRUE); // enable data field
m_ctrlSend.EnableWindow(FALSE); // enable send button
char temp[30] = "Connected to ";
char* temp2 = inet_ntoa(addrRemote.sin_addr);
strcat(temp,temp2);
m_csConnectionStatus = temp;//"Connected to " + inet_ntoa(addrRemote.sin_addr); // update status
UpdateData(FALSE);
case HELLO:
if(strncmp(m_csRecData,messageArray[iPacketCount].recvMessage,3) != 0){
AfxMessageBox("Received incorrect response from server."
// display received data and expected data in log window.
}
else{
iPacketCount++;
pObject->AutoSend();
}
break;
case MAILFROM:
if(strncmp(m_csRecData,messageArray[iPacketCount].recvMessage,3) != 0){
AfxMessageBox("Received incorrect response from server."
// display received data and expected data in log window.
}
else{
iPacketCount++;
pObject->AutoSend();
}
break;
case RCPTTO:
if(strncmp(m_csRecData,messageArray[iPacketCount].recvMessage,3) != 0){
AfxMessageBox("Received incorrect response from server."
// display received data and expected data in log window.
}
else{
iPacketCount++;
pObject->AutoSend();
}
break;
case DATA:
if(strncmp(m_csRecData,messageArray[iPacketCount].recvMessage,3) != 0){
AfxMessageBox("DATA Received incorrect response from server."
// display received data and expected data in log window.
}
pObject->EnableSendButton();
iPacketCount++;
break;
But now my program still cannot run. Can you help me to see what is the problem?
The error msg is: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
void CmyDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
Your problem is you have the thread as a class member. Can't do that. Why? Not sure yet. I think it's because each instance of a class has different member variables but they share the same member functions. If it was made virtual maybe it would work? That's why I call the thread in the class using it and pass a pointer to the class.
Take the thread out of the class. Declare it at the top and call it the same way you are now. Your .cpp file should be setup as follows:
// some .h files
UINT newThread(LPVOID); // declare thread
YourClass{
// member functions etc.
// start thread
} // end of class
UINT newThread(LPVOID pParam){
// some code here
AfxEndThread(NULL);
return 0;
}
Now the thread is not part of the class just sitting in the same file.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.