Watch (gb.inotify)
这个类表示一个被监视的文件系统对象。
Inotify事件是异步报告的,内核不能保证及时交付事件。例如,如果在您监视的目录中创建了一个文件,然后立即将其删除,则可能会在文件已删除后收到Create事件。如果你未留心尝试读取由
Watch.Name 命名的文件,你的程序将崩溃,并显示“没有这样的文件或目录”。
你应该阅读inotify手册页的“限制和注意事项”部分(`man7 inotify`)。这个手册页是一个很好的补充信息来源。
监视目录是 非 递归的, 即你将只接收来自目录本身及其直接条目的事件。如果你想更深入,你必须手动添加递归监视。
常数
静态属性
属性
方法
事件
静态属性用于在事件管理期间存储来自内核的数据。仅在事件处理程序中使用它们。
Example
Use "gb.inotify"
'The above line is required for Gambas Script only, in full Gambas IDE, see Project Components.
Private $hWatch As Watch
Public Sub Main()
Dim sFile1 As String = Temp$(), sFile2 As String = Temp$()
$hWatch = New Watch(File.Dir(Temp$()), True) As "Watcher"
' This line is redundant: a Watch object will automatically subscribe to events for which an event handler exists!
'$hWatch.Events[Watch.Create Or Watch.Delete] = True
Print "I'm watching... "
' Create two files
File.Save(sFile1, "abc")
File.Save(sFile2, "def")
Wait 0.1 ' enter event loop to see events
' Delete one file
Kill sFile2
Wait 0.1
' Delete the second file. The Delete event was unsubscribed at this point (see Watcher_Delete), so no event is raised for this.
Kill sFile1
Wait 1
Quit
End
Public Sub Watcher_Create()
Print "Create: " & Watch.Name
End
Public Sub Watcher_Delete()
Print "Delete: " & Watch.Name
' Unsubscribe from deletions after the first one.
Last.Events[Watch.Delete] = False
End
I'm watching...
Create: 1.tmp
Create: 2.tmp
Delete: 2.tmp