我正在尝试使用FindFirstChangeNotification函数查看目录的更改。如果我获取 FindFirstChangeNotification 返回的句柄并将其填充到 WaitForSingleObject 中,则此方法有效。问题是 WaitForSingleObject 会阻塞整个应用程序,直到它返回。
所以,我环顾四周,似乎RegisterWaitForSingleObject是要走的路:
Sub monitorDir(dir As FolderItem)
Declare Function FindFirstChangeNotificationW Lib "Kernel32" (dirPath As WString, watchChildren As Boolean, eventTypeFilter As Integer) As Integer
Declare Function RegisterWaitForSingleObject Lib "Kernel32" (ByRef waiterHWND As Integer, HWND As Integer, cllbck As Ptr, _
context As Integer, wait As Integer, flags As Integer) As Integer
Dim allFilters As Integer = &h00000001 Or &h00000002 Or &h00000004 Or &h00000008 Or &h00000010_
Or &h00000100
Dim monitorHandle As Integer = FindFirstChangeNotificationW(dir.AbsolutePath, True, allFilters)
If monitorHandle <> 0 Then
Call RegisterWaitForSingleObject(myCallbackHWND, monitorHandle, AddressOf MyCallbackFn, 0, &hFFFFFFFF, 0)
End Sub
这似乎在应用程序继续正常执行时起作用。但是,一旦调用 MyCallbackFn(即,当目录中发生更改时),事情就会变得……很奇怪。应用程序从 Process Explorer 和 Windows Explorer 开始崩溃或锁定。我必须退出 Windows 才能恢复。
目前,MyCallbackFn 所做的只是:
Sub MyCallbackFn()
Declare Function UnregisterWaitEx Lib "Kernel32" (waitHWND As Integer, eventHandle As Integer) As Integer
Call UnregisterWaitEx(myCallbackHWND, 0)
MsgBox("Change Detected")
End Sub
我是通过使用 RegisterWaitForSingleObject 找出错误的树,是我用错了,还是 RealBasic 中有一些限制导致回调使系统内爆?