【问题标题】:Unwiring events not working with FileSystemWatcher取消连接事件不适用于 FileSystemWatcher
【发布时间】:2011-08-15 20:52:30
【问题描述】:

我有一个应用程序正在使用 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);
            }

但它似乎不起作用,应用程序仍然进入循环。那么为什么它不起作用,我需要做什么呢?

【问题讨论】:

    标签: events c#-4.0 filesystemwatcher


    【解决方案1】:

    您可以设置而不是取消挂钩事件处理程序 FileSystemWatcher 类的 EnableRaisingEvents 属性为 false 禁用所有创建/重命名/删除/更改事件。 更改链接后,只需启用 FileSystemWatcher 通过将 EnableRaisingEvents 设置为 true。

    【讨论】:

    • 谢谢,成功了!知道为什么我第一次做的没有用吗?
    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多