// 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 portion 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;
}

ms_suzie
December 10th, 2001, 04:27 AM
by the way, the same thing happens with the COMM sample (TTY) "Serial Sample: Communications Demonstration" from MSDN.

TekBoy
December 13th, 2001, 10:18 PM
well, first of all, I would check the return value of all your functions. For example, you didn't check whether WaitForMultipleObjects() returns WAIT_FAILED.

second thing is to find out where your app stops responding. For example, I would produce a diagnostic message (i.e. TRACE) in several places of your program, and when your app freezes, look at the last diagnostic message.

ms_suzie
December 14th, 2001, 12:34 AM
thanks! that's exactly what i did. i checked for wait timeout and added trace statements to determine where the app actually stops responding. and i found a forever loop inside the read event... whew! thanks so much!

TekBoy
December 17th, 2001, 02:24 PM
it was a pleasure to help you :-)


TekBoy

P.S. please rate my post if it helped you

therockyu
January 17th, 2002, 04:29 AM
Hi,
Are you able to compile to Ms SDK sample (tty.c).
I have problem of building .exe file as below
"unresolved external symbol _main" using VC
What extra files (library,headers) should I include?
Thank you.

ms_suzie
January 17th, 2002, 07:42 PM
i didn't add any library or any thing else. i just compiled it as it is. sorry. =)

therockyu
January 17th, 2002, 09:41 PM
Never mind,thank you.
Ah, one more question?
Do you get any makefile or *.dsp from the MS SDK
sample directory?
I think I have missing some files.

ms_suzie
January 17th, 2002, 10:30 PM
yup i do have a *.dsp file and also a *.dsw file from the sdk directory. can i attach them here somewhere? or maybe you can check your msdn cd. =)

therockyu
January 18th, 2002, 04:49 AM
I m so lucky to meet you.
You just have the file that I m looking for.
Could please mail the files for me(therockyu@hotmail.com).
I m really appreciate your help.
Thank you

//JP added flex table