Waitformultipleobjects返回无效句柄

时间:2015-10-22 05:11:30

标签: multithreading visual-c++

代码如下,它是一个线程的一部分。 pFileChange-> m_hDirectory的类型为HANDLE,pFileChange-> m_eventFileChange的类型为CEvent。 CreateFile和ReadDirectoryChangesW返回成功。我无法弄清楚为什么我的手柄状态无效,请帮忙!

UINT CFileChange::FileMontiorThread(LPVOID pArgs)
{
  CFileChange* pFileChange = NULL;

  pFileChange = (CFileChange*)pArgs;

  pFileChange = (CFileChange*)pArgs;

  CString str = pFileChange->m_strDirectory;

  LPSTR  strDirectory;
  strDirectory = str.GetBuffer(str.GetLength());
  PathRemoveFileSpec(strDirectory);

  DWORD dwBytes = 0;    
  vector<BYTE> m_Buffer;
  BOOL      m_bChildren;
  OVERLAPPED    m_Overlapped;
  HANDLE arrHandles[2] = { pFileChange->m_hDirectory,  pFileChange->m_eventFileChange };

  ::ZeroMemory(&m_Overlapped, sizeof(OVERLAPPED));
  DWORD dwBufferSize = 16384;
  m_Buffer.resize(dwBufferSize);
  m_bChildren = false;


  pFileChange->m_hDirectory = ::CreateFile(
     (LPCSTR)(LPCTSTR)strDirectory,
    FILE_LIST_DIRECTORY,                
    FILE_SHARE_READ                     
    | FILE_SHARE_WRITE
    | FILE_SHARE_DELETE,
    NULL,                               
    OPEN_EXISTING,                      
    FILE_FLAG_BACKUP_SEMANTICS          
    | FILE_FLAG_OVERLAPPED,
    NULL);                              

  if (pFileChange->m_hDirectory == INVALID_HANDLE_VALUE)
  {
    return false;
  }

  BOOL success = ::ReadDirectoryChangesW(
    pFileChange->m_hDirectory,  // handle to directory
    &m_Buffer[0],                       // read results buffer
    m_Buffer.size(),                    // length of buffer
    m_bChildren,                        // monitoring option
    FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION |  FILE_NOTIFY_CHANGE_FILE_NAME,        // filter conditions
    &dwBytes,                           // bytes returned
    &m_Overlapped,                      // overlapped buffer
    NULL);           // no completion routine

  DWORD dwWaitStatus;

  while (!pFileChange->m_bKillThread)
  {
    dwWaitStatus = WaitForMultipleObjects(2, arrHandles, FALSE, INFINITE);

   Switch(dwWaitStatus)
   {
     case WAIT_FAILED:
        {
            ULONG rc = 0;
            rc = ::GetLastError();
            LPVOID lpMsgBuf = NULL;
            ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                rc,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default  language
                (LPTSTR)&lpMsgBuf,
                0,
                NULL);
            CString strErrMsg;
            strErrMsg.Format(_T("%s, %s, Reason:%s"), "", "",         (LPTSTR)lpMsgBuf);
            break;
        }
    }
  }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

请注意,正如它指定in the documentation一样,您无法等待任何类型的句柄。

wait

等待目录句柄不会按照您的想法去做。 Read this related question以及更多信息和背景阅读的答案。

您似乎正在尝试创建文件夹监视器,或许read this blog post以正确使用 var app = angular.module('myExample', []); function myCtrl($scope) { $scope.myOptions = [{ "id": 106, "group": "Group 1", "label": "Item 1" },{ "id": 107, "group": "Group 1", "label": "Item 2" },{ "id": 110, "group": "Group 2", "label": "Item 3" }]; $scope.$watch('myOption', function(result){ if (result) $scope.myOptionnew = result; }); };

答案 1 :(得分:-2)

答案是provided by Hans Passant in a comment

  

在创建它们之前,将它们复制到arrHandles中太早。那当然行不通。