ReadDirectoryChangesW和GetOverlappedResult

时间:2017-04-27 18:04:36

标签: c++ windows multithreading winapi

我异步调用ReadDirectoryChangesW来监视后台线程中的目录更改。

这是如何打开目录(basePath)和"阅读"线程已启动:

    m_hDIR = CreateFileW(
            basePath,
            FILE_LIST_DIRECTORY | GENERIC_READ,
            FILE_SHARE_WRITE | FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
            NULL);

        if (m_hDIR == INVALID_HANDLE_VALUE)
            throw CrException(CrWin32ErrorString());

        //Start reading changes in background thread
        m_Callback = std::move(a_Callback);
        m_Reading = true;
        m_ReadThread = std::thread(&CrDirectoryWatcher::StartRead, this);

这是StartRead() :(注意:m_Readingatomic<bool>

void StartRead()
        {
            DWORD dwBytes = 0;
            FILE_NOTIFY_INFORMATION fni{0};
            OVERLAPPED o{0};

            //Be sure to set the hEvent member of the OVERLAPPED structure to a unique event.
            o.hEvent = CreateEvent(0, 0, 0, 0);

            while(m_Reading)
            {
                if (!ReadDirectoryChangesW(m_hDIR,
                    &fni, sizeof(fni),
                    TRUE, FILE_NOTIFY_CHANGE_LAST_WRITE,
                    &dwBytes, &o, NULL))
                {
                    CrAssert(0, CrWin32ErrorString());
                }

                if (!GetOverlappedResult(m_hDIR, &o, &dwBytes, FALSE))
                    CrAssert(0, CrWin32ErrorString());

                if (fni.Action != 0)
                {
                    std::wstring fileName(fni.FileName, fni.FileNameLength);
                    m_Callback(fileName);
                    fni.Action = 0;
                }
            }
        }

基本上,我是&#34;民意调查&#34;每帧都有新的变化。 现在,当我调用GetOverlappedResult()时,它会失败并产生以下错误:

  

重叠的I / O事件未处于信号状态。

我错过了什么吗? ReadDirectoryChangesW是否意味着每个人都被称为&#34; tick&#34;?或者只是在检测到新的变化时?

注意:当我省略OVERLAPPED结构(和GetOverlappedResult)时,它可以工作,但会阻塞线程直到读取更改。这可以防止我的应用程序正确终止。 (即我无法加入该主题)

1 个答案:

答案 0 :(得分:6)

调用GetOverlappedResult()时,如果将bWait参数设置为FALSE并且I / O操作尚未完成,GetOverlappedResult()将失败并显示ERROR_IO_INCOMPLETE错误代码:

  

bWait [in]
  如果此参数为TRUE,并且Internal结构的lpOverlapped成员为STATUS_PENDING,则该函数在操作完成之前不会返回。 如果此参数为FALSE且操作仍处于暂挂状态,则函数返回FALSEGetLastError函数返回ERROR_IO_INCOMPLETE

这不是致命错误,所以请忽略该错误并继续前进。

是的,请确保在ReadDirectoryChangesW()报告先前完成先前的I / O操作之前不再调用GetOverlappedResult()

现在,说到这一点,您的代码还有另一个问题。您的线程正在堆栈上分配单个FILE_NOTIFY_INFORMATION实例。如果您查看FILE_NOTIFY_INFORMATION的定义,其FileName字段是可变长度的:

typedef struct _FILE_NOTIFY_INFORMATION {
  DWORD NextEntryOffset;
  DWORD Action;
  DWORD FileNameLength;
  WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
  

文件名
  可变长度字段,包含相对于目录句柄的文件名。文件名采用Unicode字符格式,不以空值终止。

这意味着静态分配FILE_NOTIFY_INFORMATION将会太小,dwBytes几乎总是0,因为ReadDirectoryChangesW()将无法返回完整FILE_NOTIFY_INFORMATION对你来说(除非FileName的长度恰好是1个字符):

  

首次调用ReadDirectoryChangesW时,系统会分配缓冲区来存储更改信息。此缓冲区与目录句柄关联,直到它关闭并且其大小在其生命周期内不会更改。在对此函数的调用之间发生的目录更改将添加到缓冲区,然后在下一次调用时返回。 如果缓冲区溢出,则丢弃缓冲区的全部内容,lpBytesReturned参数包含零,ReadDirectoryChangesW函数失败,错误代码为ERROR_NOTIFY_ENUM_DIR

  

ERROR_NOTIFY_ENUM_DIR
  1022(0x3FE)
  正在完成通知更改请求,并且未在调用方缓冲区中返回信息。 调用者现在需要枚举文件以查找更改

因此,您需要为接收FILE_NOTIFY_INFORMATION数据动态分配大字节缓冲区,然后只要GetOverlappedResult()报告该数据可用,就可以遍历该缓冲区。

你的主题应该看起来像这样:

void StartRead()
{
    DWORD dwBytes = 0;
    std::vector<BYTE> buffer(1024*64);
    OVERLAPPED o{0};
    bool bPending = false;

    //Be sure to set the hEvent member of the OVERLAPPED structure to a unique event.
    o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!o.hEvent) {
        CrAssert(0, CrWin32ErrorString());
    }

    while (m_Reading)
    {
        bPending = ReadDirectoryChangesW(m_hDIR,
            &buffer[0], buffer.size(),
            TRUE, FILE_NOTIFY_CHANGE_LAST_WRITE,
            &dwBytes, &o, NULL);
        if (!bPending)
        {
            CrAssert(0, CrWin32ErrorString());
        }

        while (m_Reading)
        {
            if (GetOverlappedResult(m_hDIR, &o, &dwBytes, FALSE))
            {
                bPending = false;

                if (dwBytes != 0)
                {
                    FILE_NOTIFY_INFORMATION *fni = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&buffer[0]);
                    do
                    {
                        if (fni->Action != 0)
                        {
                            std::wstring fileName(fni->FileName, fni->FileNameLength);
                            m_Callback(fileName);
                        }

                        if (fni->NextEntryOffset == 0)
                            break;

                        fni = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(reinterpret_cast<BYTE*>(fni) + fni->NextEntryOffset);
                    }
                    while (true);
                }

                break;
            }

            if (GetLastError() != ERROR_IO_INCOMPLETE) {
                CrAssert(0, CrWin32ErrorString());
            }

            Sleep(10);
        }

        if (bPending)
        {
            CancelIo(m_hDIR);
            GetOverlappedResult(m_hDIR, &o, &dwBytes, TRUE);
        }
    }

    CloseHandle(o.hEvent);
}

在不定期轮询I / O状态的情况下实现此方法的另一种方法是摆脱m_Reading并使用等待事件。当操作系统调用GetOverlappedResult()或终止时,让操作系统发出信号,这样它可以在其余时间没有忙碌的情况下休眠:

m_hDIR = CreateFileW(
            basePath,
            FILE_LIST_DIRECTORY | GENERIC_READ,
            FILE_SHARE_WRITE | FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
            NULL);

if (m_hDIR == INVALID_HANDLE_VALUE)
    throw CrException(CrWin32ErrorString());

m_TermEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!m_TermEvent)
    throw CrException(CrWin32ErrorString());

//Start reading changes in background thread
m_Callback = std::move(a_Callback);
m_ReadThread = std::thread(&CrDirectoryWatcher::StartRead, this);

...

SetEvent(m_TermEvent);
m_ReadThread.join();

void StartRead()
{
    DWORD dwBytes = 0;
    std::vector<BYTE> buffer(1024*64);
    OVERLAPPED o{0};
    bool bPending = false, bKeepRunning = true;

    //Be sure to set the hEvent member of the OVERLAPPED structure to a unique event.
    o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!o.hEvent) {
        CrAssert(0, CrWin32ErrorString());
    }

    HANDLE h[2] = {o.hEvent, h_TermEvent};

    do
    {
        bPending = ReadDirectoryChangesW(m_hDIR,
            &buffer[0], buffer.size(),
            TRUE, FILE_NOTIFY_CHANGE_LAST_WRITE,
            &dwBytes, &o, NULL);
        if (!bPending)
        {
            CrAssert(0, CrWin32ErrorString());
        }

        switch (WaitForMultipleObjects(2, h, FALSE, INFINITE))
        {
            case WAIT_OBJECT_0:
            {
                if (!GetOverlappedResult(m_hDIR, &o, &dwBytes, TRUE)) {
                    CrAssert(0, CrWin32ErrorString());
                }

                bPending = false;

                if (dwBytes == 0)
                    break;

                FILE_NOTIFY_INFORMATION *fni = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&buffer[0]);
                do
                {
                    if (fni->Action != 0)
                    {
                        std::wstring fileName(fni->FileName, fni->FileNameLength);
                        m_Callback(fileName);
                    }

                    if (fni->NextEntryOffset == 0)
                         break;

                    fni = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(reinterpret_cast<BYTE*>(fni) + fni->NextEntryOffset);
                }
                while (true);

                break;
            }

            case WAIT_OBJECT_0+1:
                bKeepRunning = false;
                break;

            case WAIT_FAILED:
                CrAssert(0, CrWin32ErrorString());
                break;
        }
    }
    while (bKeepRunning);

    if (bPending)
    {
        CancelIo(m_hDIR);
        GetOverlappedResult(m_hDIR, &o, &dwBytes, TRUE);
    }

    CloseHandle(o.hEvent);
}