How does FileSystemWatcher work on another computers directory?

有些话、适合烂在心里 提交于 2019-12-01 17:37:19

Using FileSystemWatcher on another computer seems too easy to be true...

It kinda is. The underlying API - ReadDirectoryChanges() - opens up a connection to the server, which is responsible for responding when something changes. If that connection gets dropped for some reason, or you bump up against the connection limit of the OS you're connecting to, then you don't get notifications.

I've found that it is more reliable to poll periodically (with some rather long interval), and use FileSystemWatcher only as a way of responding quickly to changes in between polls.

I think FileSystemWatcher does use an observer/observable pattern based on the underlying Win32 API. Not sure of the actual events but presumably when you initialise a FileSystemWatcher object to watch a network path and set the EnableRaisingEvents property it attaches itself behind the scenes to the folder as an observer by attaching to the events raised by the Win32 API as they would if the folder was on the same computer.

In the Observer design pattern, the subject (observable) is never aware of the number of or types of observer's therefore the point about the other computer knowing when the FSW is no longer watching is irrelevant.

If the network drops off during an event being raised that is a bit of a gotcha - someone else will have to help you there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!