vc串口问题,关于WaitForSingleObject(),这个函数的强解,??

winspeed 2002-05-08 04:22:29
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
这个函数什么意思,,看了n久也好像很糊涂,,高手能不能,来点详细的中文说明,,最好有例子!!!谢谢了
...全文
428 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
quanxiongwei 2002-05-09
  • 打赏
  • 举报
回复
使用::WaitForSingleObject要避免传递了一个无效线程句柄的麻烦。
传递给它的第一个参数是欲使其等待的对象的句柄。(还可以是一个进程句柄,同步化对象句柄或文件修改通知句柄,等等)
第二个参数是希望等待的时间长度。当指定了INFINITE之后,如果正在等待的对象永远不会进入信号发出状态。
可以通过检查返回值来确定函数返回的原因,WAIT_OBJECT_0意味着对象进入了信号发出状态,而WAIT_TIMEOUT表示还没有进入。
qiuanhong 2002-05-09
  • 打赏
  • 举报
回复
HANDLE m_hWriteEvents; // 写事件

if (m_hWriteEvents != NULL)
ResetEvent(m_hWriteEvents);
m_hWriteEvents = CreateEvent(NULL,TRUE,FALSE,NULL);

//在函数中
{
.......
WaitForSingleObject(m_hWriteEvents, INFINITE);
.......
}

// 在在你认为需要做 WaitForSingleObject(m_hWriteEvents, INFINITE)
后面的事情时

//启动写事件
SetEvent(m_hWriteEvents);


================================================================

附:
WaitForSingleObject
The WaitForSingleObject function returns when one of the following occurs:

The specified object is in the signaled state.
The time-out interval elapses.
DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds
);

Parameters
hHandle
Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.
Windows NT: The handle must have SYNCHRONIZE access. For more information, see Standard Access Rights.

dwMilliseconds
Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
Return Values
If the function succeeds, the return value indicates the event that caused the function to return. This value can be one of the following.

Value Meaning
WAIT_ABANDONED The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.
WAIT_OBJECT_0 The state of the specified object is signaled.
WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled.


If the function fails, the return value is WAIT_FAILED. To get extended error information, call GetLastError.

Remarks
The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters an efficient wait state. The thread consumes very little processor time while waiting for the object state to become signaled or the time-out interval to elapse.

Before returning, a wait function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one.

The WaitForSingleObject function can wait for the following objects:

Change notification
Console input
Event
Job
Mutex
Process
Semaphore
Thread
Waitable timer
For more information, see Synchronization Objects.

Use caution when calling the wait functions and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. A thread that uses a wait function with no time-out interval may cause the system to become deadlocked. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForSingleObject.

Windows CE: Windows CE does not support waiting for semaphores, change notification objects, console input, and timers.

Waiting on an invalid handle causes WaitForSingleObject to return WAIT_FAILED.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.

See Also
Synchronization Overview, Synchronization Functions, CancelWaitableTimer, CreateEvent, CreateFile, CreateMutex, CreateProcess, CreateRemoteThread, CreateSemaphore, CreateThread, CreateWaitableTimer, FindFirstChangeNotification, GetStdHandle, MsgWaitForMultipleObjects, MsgWaitForMultipleObjectsEx, OpenEvent, OpenMutex, OpenProcess, OpenSemaphore, OpenWaitableTimer, PulseEvent, ResetEvent, SetEvent, SetWaitableTimer





kissfire 2002-05-09
  • 打赏
  • 举报
回复
AaronLiu(放不稳的鸡蛋),windows的事件函数(SetEvent)不是每次都调用成功,在编程中要特别注意,我在这上面太相信微软了,害得我好苦呀。搞不好你的线程可能就挂在那里什么都干不了啦

我正好碰到你提醒的问题,我怀疑真的是SetEvent调用没有成功,我的程序老是运行到一半时死掉了,怎么判断setevent调用成功啊?
AaronLiu 2002-05-09
  • 打赏
  • 举报
回复
waitforsingleobject 是线程用于线程同步的函数,一般和SetEvent函数协调工作,Waitforsingleobject的第一个参数为一句柄HANDLE,第二个为Timeout值,一毫秒为单位,也可是无限INFINITE.
从你的应用看,对串口操作你可以用两个线程处理,ReadComm, WriteComm,但在应用中你要考虑互斥的问题,另外提醒你一个问题,windows的事件函数(SetEvent)不是每次都调用成功,在编程中要特别注意,我在这上面太相信微软了,害得我好苦呀。搞不好你的线程可能就挂在那里什么都干不了啦
qsfsea 2002-05-09
  • 打赏
  • 举报
回复
ClearCommError( m_hComDev, &dwErrorFlags, &ComStat ); //msdn
if( !ComStat.cbInQue ) return 0;//看读缓存区有多少个字节

dwBytesRead = min(dwBytesRead,(DWORD) ComStat.cbInQue);//取较小的一个,比如你要读10个字节,读缓存区里只有5个字节,你只能读5个了

bReadStatus = ReadFile( m_hComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );//&m_OverlappedRead 异步结构
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){ //还没读完呢
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );//等待2000,如果m_OverlappedRead.hEvent有信号或超时就结束,说明读完了
return dwBytesRead;// 这里不太对,应该判断一下是超时还是完成结束的,要是超时的话,dwBytesRead就不对拉
}
tianlinyi 2002-05-09
  • 打赏
  • 举报
回复
说的这么详细了呀?

呵呵
没得说了

我前几天还用这个来着
winspeed 2002-05-09
  • 打赏
  • 举报
回复
在帮我看看,,这段代码:
DWORD CSerial::ReadData( void *buffer, DWORD dwBytesRead)
{

if( !m_bOpened || m_hComDev == NULL ) return 0;

BOOL bReadStatus;
DWORD dwErrorFlags;
COMSTAT ComStat;

ClearCommError( m_hComDev, &dwErrorFlags, &ComStat ); //是不是读取串口状态,???
if( !ComStat.cbInQue ) return 0;//读缓存区是否为o????

dwBytesRead = min(dwBytesRead,(DWORD) ComStat.cbInQue);//这是什么意思,

bReadStatus = ReadFile( m_hComDev, buffer, dwBytesRead, &dwBytesRead, &m_OverlappedRead );//&m_OverlappedRead 这个什么意思?
if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );//还是不理解????
return dwBytesRead;
}
xuying 2002-05-08
  • 打赏
  • 举报
回复
这个函数是将线程阻塞,直到 m_OverlappedRead.hEvent被置位或者阻塞时间超过2000ms。它的返回值会说明是发生了哪种情况。

比如你又两个线程,一个读缓冲区,一个写缓冲区。读的时候如果缓冲区为空,你就需要等待,就可以调用这个函数。你在写缓冲区的的线程中写完后,setevent(m_OverlappedRead.hEvent),就会通知读缓冲区的线程,现在有数据可以读了,不必阻塞了。
SeaFish 2002-05-08
  • 打赏
  • 举报
回复
第一个参数:要等待的事件
第二个参数:超时值(豪秒)
m_OverlappedRead是一个异步结构,常于ReadFile()或WriteFile(),异步读写是,当操作完成后m_OverlappedRead.hEvent会变成有信号,如果你调用
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 )在没有超时的情况下返回WAIT_OBJECT_0,就是说操作已经完成了。

yu_hl 2002-05-08
  • 打赏
  • 举报
回复
WaitForSingleObject等待某个对象,等待时间为第二个参数。
如果在这段时间内对象有信号了,WaitForSingleObject就返回,否则阻塞在那里直到超时。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧