具有bWait = false的GetOverlappedResults块

时间:2014-10-12 15:58:07

标签: c++ asynchronous overlapped-io

GetOverlappedResults()根本没有回复 我运行了简单的示例,当网络接口中有IP地址更改时,手动重置事件已设置,我可以看到“IP地址表已更改...”输出,但即使bWait为false,GetOverlappedResults()也不会返回。即使bWait = true,它也应该返回,因为事件已设置,因此I / O操作已完成。

发生了什么事?

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <windows.h>

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

void main()
{
    OVERLAPPED overlap;
    DWORD ret, nr;
    HANDLE hand = NULL;
    overlap.hEvent = CreateEvent(NULL, true, false, NULL);

    ret = NotifyAddrChange(&hand, &overlap);

    for (;;)
    {
        if ( WaitForSingleObject(overlap.hEvent, INFINITE) == WAIT_OBJECT_0 )
        {
            printf("IP Address table changed..\n");
            ret = GetOverlappedResult(hand, &overlap, &nr, false);
            scanf_s("%d %d\n", ret, nr);
            printf("done\n");
            NotifyAddrChange(&hand, &overlap);
            ResetEvent(overlap.hEvent);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

等待是由scanf_s()引起的。我想你的意思是printf返回值不读它。