Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multithread sample

Status
Not open for further replies.

joice

Programmer
Nov 22, 2000
11
SG
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);

// start receive thread
CWinThread* pRecvThread = AfxBeginThread(ReceiveThread,(LPVOID)this);

// update dialog
m_ctrlConnect.EnableWindow(FALSE);
UpdateData(FALSE);
return 0;
}




UINT ReceiveThread(LPVOID pParam)
{
CSMTPClientDlg* pObject = (CSMTPClientDlg*)pParam;

do{
m_csRecData = Sock1.RecData();
cdLogging.SetText(m_csRecData);
switch(iPacketCount)
{
case SERVER:
iPacketCount++;
pObject->AutoSend();
break;

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;

default:
;
}
}while(bExitThread == FALSE);
AfxEndThread(NULL);
return 0;
}
 
Hi:
Thank you very much for your sample.

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

// some code here

// start receive thread
CWinThread* pRecvThread = AfxBeginThread(newThread,(LPVOID)this);
}

UINT CmyDlg::newThread(LPVOID pParam){
// some code here

AfxEndThread(NULL);

return 0;

}

Thank you!
 
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.

Have fun,

Brother C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top