取消连接事件不适用于 FileSystemWatcher
我有一个使用 FileSystemWatcher 来监视文件夹中文件更改的应用程序。问题是,当它捕获这些事件时,它需要对这些文档进行更改(更新链接),这当然会再次触发事件,使应用程序陷入循环。
所以我尝试了这个:
UnWireEvents(); //Turn off the events while updating the documents
ChangeAllLinks();
WireEvents(); //Turn the events back on
private void WireEvents()
{
_monitor.FileChanged += new EventHandler(_monitor_FileChanged);
}
private void UnWireEvents()
{
_monitor.FileChanged -= new EventHandler(_monitor_FileChanged);
}
但它似乎不起作用,应用程序仍然进入循环。那么为什么它不起作用,我需要做什么呢?
I have an application that is using FileSystemWatcher to monitor a folder for changes to files. The problem is that when it catches these events, it needs to do changes to those documents (updating links), and this of course triggers the events again, throwing the application into a loop.
So I tried this:
UnWireEvents(); //Turn off the events while updating the documents
ChangeAllLinks();
WireEvents(); //Turn the events back on
private void WireEvents()
{
_monitor.FileChanged += new EventHandler(_monitor_FileChanged);
}
private void UnWireEvents()
{
_monitor.FileChanged -= new EventHandler(_monitor_FileChanged);
}
But it doesn't seem to work, the application still goes into a loop. So why doesn't it work, and what do I need to do instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置,而不是取消事件处理程序
将 FileSystemWatcher 类的 EnableRaisingEvents 属性设置为 false
禁用所有创建/重命名/删除/更改事件。
更改链接后,只需启用 FileSystemWatcher
通过将 EnableRaisingEvents 设置为 true。
Instead of unhooking your event handler you could set
the EnableRaisingEvents property of the FileSystemWatcher class to false
disabling all Create/Rename/Delete/Change events.
After you have changed your links simply enable the FileSystemWatcher
by setting EnableRaisingEvents to true.