// JP opened flex table

Click to See Complete Forum and Search --> : multithreading problem!!!


ms_suzie
December 10th, 2001, 02:31 AM
help anyone!

i'm tearing my hair out on this one. i've a problem with my class for serial communications. while my app is receiving a long file, i alternately connect to and disconnect from my source. however, around the 6th or 7th time i try to disconnect, my app stops responding. i don't know where the problem is. here's a "small" porion of my code. any suggestion or comment is very much welcome! anybody! thanks! =)



unsigned int CSerial::CommWatchProc(void *pParam)
{
CSerial *port = (CSerial*)pParam;

port->m_bThreadAlive = true;
port->m_objThread->m_bAutoDelete = true;

// Misc. variables
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
BOOL bResult = TRUE;


while (port->m_bThreadAlive)
{
bResult = WaitCommEvent(port->m_hComm, &Event, &port->m_ov);

if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// Do nothing and continue
break;
}
case ERROR_INVALID_PARAMETER: //The parameter is incorrect.
{
// Recoverable Error!
port->PrintError("CommWatchProc",dwError);
break;
}
default:
{
// Process this error.
port->PrintError("WaitCommEvent()",dwError);
break;
}
}
}
else
{
bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

if (comstat.cbInQue == 0)
continue;
}

// Main wait function. This function will normally block the thread
// until one of nine events occur that require action.

Event = WaitForMultipleObjects(3, port->m_hEventArray, FALSE, INFINITE);

switch (Event)
{
case 0: // Shutdown event.
{
TRACE("Thread Kill Event Received\n");
port->m_bThreadAlive = false;

AfxEndThread(100);
}
case 1: // read event
{
GetCommMask(port->m_hComm, &CommEvent);

if (CommEvent & EV_RXCHAR && port->m_bRcvEnable)
ReceiveChar(port, comstat);

break;
}
case 2: // write event
{
WriteChar(port);
break;
}
}
}

return 0;
}

//JP added flex table