【问题标题】:FilesystemWatcher raising even after disposedFilesystemWatcher 即使在处置后也会提高
【发布时间】:2014-06-10 04:18:29
【问题描述】:

我已经正确地将 EnableRaisingEvents 设置为 false,但它会引发事件,这就是我的做法:

    private void button2_Click(object sender, EventArgs e)
    {
        _watcher.EnableRaisingEvents = false;
        _watcher.Changed -= new FileSystemEventHandler(InitList);
        _watcher.Dispose();
        listBox1.Items.Add("Stopped Monitoring Directory " + textBox2.Text);
        listBox1.SelectedIndex = listBox1.Items.Count - 1;
        textBox2.Enabled = true;
        textBox3.Enabled = true;
        button1.Enabled = true;
        button3.Enabled = true;
        button4.Enabled = true;

    }

还有什么需要做的吗,也许我在这里错过了。请指教。 http://pastebin.com/yEaffBxP

【问题讨论】:

  • 可能是在检测到您说“取消监控”之前发生的引发事件?
  • 这仍在测试中,所以我可以在监控它的同时完全控制它。所以我确定没有引发任何事件。在我按下按钮然后尝试引发事件后,filesystemwatcher 仍在工作
  • 首先,我已经看到了,您实际上已经创建了 2 个文件监视程序。一个在这里private static FileSystemWatcher _watcher = new FileSystemWatcher();,第二个 - 稍后在 WatchFile() 中。正在阅读...
  • 最好不要使用静态变量,否则你很可能会跳过数据。将其存储在静态全局变量中在逻辑上也是没有意义的,这也破坏了封装和真正的 OO 实践。

标签: c# .net dispose filesystemwatcher


【解决方案1】:

我已经看到了,您实际上已经创建了 2 个文件监视程序。 一个来了

private static FileSystemWatcher _watcher = new FileSystemWatcher();

秒 - 稍后

WatchFile()

应该可以解决

【讨论】:

    【解决方案2】:

    要停止观察者,请将其 EnableRaisingEvents 属性设置为 False。如果你已经完成了它,那么就丢弃它。

    【讨论】:

    • 你能给我举个例子吗,我已经将它设置为 false,但我在它之后调用 dispose。应该调用 dispose 的位置
    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多