VC 调用WaitforSingleObject报错.该怎么处理
1个回答
2016-04-25 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
////////参考:C/C++ code///////////////////////////////////////////////////////
int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
int WaitErr;
BOOL fReadStart;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength;
if (!m_bConnected) return 0;
if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat))
{ //ComStat filled
if (dwErrorFlags)
{
if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow");
else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error");
else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error");
else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error");
else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected");
else AfxMessageBox("CE_OTHERS");
}
}
// nBlockLen may >,=,< ComStat.cbInQue !
dwLength=min((DWORD)nBlockLen,ComStat.cbInQue);
if(dwLength>0)
{ // read required
fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
} //end if dwLength>0
return dwLength;
}
// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS)
{ // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
int CUload::ReadCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
int WaitErr;
BOOL fReadStart;
COMSTAT ComStat;
DWORD dwErrorFlags,dwLength;
if (!m_bConnected) return 0;
if (ClearCommError(m_idComDev,&dwErrorFlags,&ComStat))
{ //ComStat filled
if (dwErrorFlags)
{
if (dwErrorFlags & CE_RXOVER) AfxMessageBox("Receive Queue overflow");
else if(dwErrorFlags & CE_OVERRUN) AfxMessageBox("Receive Overrun Error");
else if(dwErrorFlags & CE_RXPARITY) AfxMessageBox("Receive Parity Error");
else if(dwErrorFlags & CE_FRAME ) AfxMessageBox("Receive Framing error");
else if(dwErrorFlags & CE_BREAK) AfxMessageBox("Break Detected");
else AfxMessageBox("CE_OTHERS");
}
}
// nBlockLen may >,=,< ComStat.cbInQue !
dwLength=min((DWORD)nBlockLen,ComStat.cbInQue);
if(dwLength>0)
{ // read required
fReadStart=ReadFile(m_idComDev,pBlock,dwLength,&dwLength,&m_osRead);
if(!fReadStart)
{ // if there was a problem, or the async. operation's still pending ...
if(GetLastError()==ERROR_IO_PENDING)
{ // asynchronous i/o is still in progress
if (WaitErr=WaitForSingleObject(m_osRead.hEvent,60000))// 1 minute
{ // time over
if(WaitErr==WAIT_TIMEOUT)
{ // time out
dwLength=0;
AfxMessageBox("Time out !");
}// end time out
}// end waiterr
} // end io_pending
else
{ // WaitErr=0.if you SetEvent(m_osRead.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osRead,&dwLength,FALSE);
m_osRead.Offset +=dwLength;
}
}
} //end if dwLength>0
return dwLength;
}
// an work thread to monitor the _CTS and RX_CHAR
UINT CUload::CommWatchRead(LPVOID lpParam)
{ // return 0=OK 1=Error
CUload *pUload=(CUload*)lpParam;
OVERLAPPED os;
DWORD dwEventMask,dwTransfer;
memset(&os,0,sizeof(OVERLAPPED));
os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);// attrb,Manual,init,name
if (os.hEvent==NULL)
{
AfxMessageBox("Can't create Event",MB_ICONSTOP);
return 1;// error
}
// set 2 events !
if (!SetCommMask(pUload->m_idComDev,EV_RXCHAR|EV_CTS)) return 1;
while (pUload->m_bConnected)
{ // break,only if m_bConnected=NULL
dwEventMask=0;
if(!WaitCommEvent(pUload->m_idComDev,&dwEventMask,&os))
{ // function fails
if (GetLastError()==ERROR_IO_PENDING)
{// TRUE=WaitForSingleObject() should be called inside
GetOverlappedResult(pUload->m_idComDev,&os,&dwTransfer,TRUE);
os.Offset+=dwTransfer;
}
}
if((dwEventMask & EV_RXCHAR)==EV_RXCHAR)
{ // some chats received.
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)1, // EV_RXCHAR
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
// if not at same time,use else if
if((dwEventMask & EV_CTS)==EV_CTS)
{ // CTS changed
ResetEvent(pUload->m_hPostEventRead);// first reset
pUload->PostMessage(WM_COMMNOTIFY,
(WPARAM)0, // EV_CTS
(LPARAM)0); // not used
// wait answer
WaitForSingleObject(pUload->m_hPostEventRead,0xFFFFFFFF);
}
}
CloseHandle(os.hEvent);
return 0;// OK
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询