读书人

高手赐招2个线程崩溃了,该怎么处理

发布时间: 2012-02-20 21:18:24 作者: rapoo

高手赐招,2个线程崩溃了
DWORD CWorker::SendCommand( )
{
DWORD dwRet = 0;

bool bIsNeedDelayForCommand = false;

CommandInfoLine LastCommandInfo;

while( theIsConnectOK )
{

EnterCriticalSection( &m_CriticalSection );
GetNextCommandFromCommandList( m_CurCommandInfo ); //
LeaveCriticalSection( &m_CriticalSection );

if ( IsNeedDelayForCommand( LastCommandInfo ) )
Sleep( theCycle );

WaitForSingleObject( m_hSend, INFINITE );

Sleep( 100 ); //wait 100ms.

ResetEvent( m_hSend ); //rob line.

if ( m_CurCommandInfo.strCommandID == "30 " )
PostMessage( theWnd, MESSAGE_SENDING_CONNECT, NULL, NULL );

else if ( m_CurCommandInfo.strCommandID == "31 " )
PostMessage( theWnd, MESSAGE_SENDING_USERCOMMAND, NULL, NULL );

else if ( m_CurCommandInfo.strCommandID == "32 " )
PostMessage( theWnd, MESSAGE_SENDING_OPENDOOR, NULL, NULL );

else if ( m_CurCommandInfo.strCommandID == "33 " )
PostMessage( theWnd, MESSAGE_SENDING_QUERYSTATUS, NULL, NULL );

else if ( m_CurCommandInfo.strCommandID == "34 " )
PostMessage( theWnd, MESSAGE_SENDING_QUERYRECORD, NULL, NULL );

else if ( m_CurCommandInfo.strCommandID == "3F " )
PostMessage( theWnd, MESSAGE_SENDING_ALTERNO, NULL, NULL );


SetEvent( m_hReceive );

theCom.WriteInfo( m_CurCommandInfo.strCommandLine.c_str( ) );

WaitForSingleObject( m_hReceive, INFINITE );

memset( &LastCommandInfo, 0, sizeof( LastCommandInfo ) );

LastCommandInfo = m_CurCommandInfo;
}

return dwRet;
}

DWORD CWorker::MonitorDevice( )
{
DWORD dwRet = 0;

while( theIsConnectOK )
{
WaitForSingleObject( m_hSend, INFINITE );

WaitForSingleObject( m_hReceive, INFINITE );

ResetEvent( m_hReceive );

DWORD dwEvtMask = 0, dwError = 0;
OVERLAPPED OverLapped;
COMSTAT comStat;

CHAR buffer[ 20480 ]; //in-buffer
memset( &OverLapped, 0, sizeof( OverLapped ) );
OverLapped.hEvent = CreateEvent( NULL, true, false, NULL );



WaitCommEvent( theCom.GetDeviceHandle( ), &dwEvtMask, &OverLapped ); //wait until EV_RXCHAR.

if ( WaitForSingleObject( OverLapped.hEvent, m_CurCommandInfo.dwTimeOut ) == WAIT_TIMEOUT ) //time-out
{

EnterCriticalSection( &m_CriticalSection );
m_CurCommandInfo.dwStatus = 0; //command fail.
LeaveCriticalSection( &m_CriticalSection );


PostMessage( theWnd, MESSAGE_COMMAND_FAIL, NULL, NULL );

FinishCommand( );
}
else
{
ClearCommError( theCom.GetDeviceHandle( ), &dwError, &comStat );

theCom.ReadInfo( buffer ); //read information from in-buffer.

EnterCriticalSection( &m_CriticalSection );
m_CurCommandInfo.dwStatus = 1; //command fail.
LeaveCriticalSection( &m_CriticalSection );

PostMessage( theWnd, MESSAGE_COMMAND_OK, NULL, NULL );

DealWithReceivedInfo( buffer );

FinishCommand( );
}

//Sleep( 300 );

CloseHandle( OverLapped.hEvent );
}

return dwRet;
}
请高手们此招了,崩溃信息:Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.




[解决办法]
ESP错误?
检查一下你的代码,调用方式是否正确.stdcall cdecll?
[解决办法]
恭喜楼主自己解决问题!散个分吧。

读书人网 >C++

热点推荐