-
November 28th, 2003, 06:23 AM
#1
need help using thread and socket
hi
i have developed a server socket program which keeps on sending message. i have created a seperate thread using afxbeginthread(SendMsg,this) (this points to the CSocket derived class). im using carchive. the problem is when the thread tries to send messaage i get a debug assertion error.
i dont know what is the cause of the problem.
UINT SendMS(LPVOID lp)
{
CServerSock* pSock=(CServerSock*)lp;
do
{
pSock->SendStatus();// here i get error
Sleep(1000);
}while(pSock->m_pDlg->m_sta);
return 1;
}
void CServerSock::SendStatus()
{
m_pDlg->Serialize(*m_pArOut);
m_pArOut->Flush();
}
when i press retry i goes to base class
void CAsyncSocket::AssertValid() const
{
CObject::AssertValid();
ASSERT(m_hSocket == INVALID_SOCKET || CAsyncSocket::FromHandle(m_hSocket) != NULL);
}
outside the thread im able to send the message. i would be happy if u can help me with this problem
regards
aswin
-
November 28th, 2003, 06:36 AM
#2
Code:
CServerSock* pSock=(CServerSock*)lp;
do NOT pass pointers to MFC objects across threads! NEVER!
pass the m_hWnd's of the objects instead.
see this FAQ and this thread.
-
November 28th, 2003, 11:57 AM
#3
Post the code the calls the API to send messages. One solution is AfxGetMainWnd().
Kuphryn
-
November 28th, 2003, 02:02 PM
#4
The code in the thread will be running in a different 'scope' from the MFC object you created, and therefore the pointer is invalid.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|