【问题标题】:FilesystemWatcher firing events only onceFilesystemWatcher 仅触发一次事件
【发布时间】:2023-03-21 20:05:05
【问题描述】:

我正在构建一个 c# 应用程序,该应用程序将具有单独的 FilesystemWatcher 实例来监视多个文件夹并更新它的关联列表框。

   public class MyFileWatcher
{
    private TextBox _textBox;
    private ListBox _listBox;
    private string _folderDestination;
    FileSystemWatcher  _watcher;


    public  MyFileWatcher(TextBox textBox, ListBox listBox, string destfolderTextBox , System.ComponentModel.ISynchronizeInvoke syncObj)
    {
        this._textBox = textBox;
        this._listBox = listBox;
    this._folderDestination = destfolderTextBox;

        this._watcher = new FileSystemWatcher();
      //  this._watcher.SynchronizingObject = syncObj;
        this._watcher.Changed += new FileSystemEventHandler(convertXML);

        this._watcher.IncludeSubdirectories = false;
        this._watcher.Path = textBox.Text;
        this._watcher.EnableRaisingEvents = true;

        // add any other required initialization of the FileSystemWatcher here. 

    }

    public void WatchFile(TextBox ctrlTB, ListBox ctrlLB)
    {
        // FileSystemWatcher _watcher = new FileSystemWatcher();
        //var localTB = ctrlTB as TextBox;
        //var localLB = ctrlLB as ListBox;
        _watcher.Path = ctrlTB.Text;
        _watcher.Path = ctrlTB.Text;



        _watcher.NotifyFilter = NotifyFilters.LastWrite;
        _watcher.Filter = "*.xml";

        _watcher.Changed += new FileSystemEventHandler(convertXML);
        // _watcher.Changed += (s, e) => convertXML(s,e); 
        // _watcher.Error += new ErrorEventHandler(WatcherError);
        _watcher.EnableRaisingEvents = true;
        _watcher.IncludeSubdirectories = false;


        ctrlLB.Items.Add("Started Monitoring @ " + ctrlTB.Text);
        ctrlLB.SelectedIndex = ctrlLB.Items.Count - 1;
    }

这被解雇了:

 public void button9_Click(object sender, EventArgs e)
    {
        if (!Directory.Exists(this.textBox1.Text))
        {
            //Form2.ActiveForm.Text = "Please select Source Folder";
            // popup.Show("Please Select Source Folder");
            MessageBox.Show("Please Select Proper Source Folder");
            return;
        }

        else
        {
            textBox1.Enabled = false;

            button9.Enabled = false;
            button1.Enabled = false;
          //  button4.Enabled = false;
         //   FileSystemWatcher _watcher = new FileSystemWatcher();
          //  _watcher.SynchronizingObject = this;
         //  WatchFile(textBox1,listBox1 ,_watcher);
            //object syncobj = Form1.ActiveForm;
            string destfolder = textBox7.Text + "\\";
            destfolder += "test.xml";
            MyFileWatcher myWatcher = new MyFileWatcher(textBox1, listBox1, destfolder, this);

          myWatcher.WatchFile(textBox1, listBox1);

        }
    }

现在当它被触发时。它第一次工作,但它没有拿起第二次。我觉得 _watcher 实例正在被垃圾收集。但我不明白的是它在 MyFileWatcher 类中被声明为全局。

【问题讨论】:

  • 您在button9_Click 中创建了一个局部变量myWatcher(所以,MyFileWatcher 可能会被垃圾回收,是的。)。如果您将 MyFileWatcher 添加为字段,会发生什么情况?
  • 但是myWatcher 是一个可以收集的局部变量。它没有实现IDisposable(它应该实现),但最终FileSystemWatcher 也会被收集。
  • @Dirk 我该如何阻止这种情况的发生
  • @user726720 我说的不是_watcher 字段,我说的是button9_Click 方法的myWatcher 局部变量。

标签: c# .net winforms filesystemwatcher


【解决方案1】:
MyFileWatcher myWatcher = new MyFileWatcher(textBox1, listBox1, destfolder, this);

因为这是一个局部变量并且MyFileWatcher 拥有FileSystemWatcher,所以它会被垃圾回收。

如果您将 myWatcher 添加为字段,它应该可以正常工作。

【讨论】:

  • 谢谢我已经做到了。我是否必须为每个实例声明 myWatcher 字段。我预计至少有 7 个实例同时运行。
  • 你的意思是如果你想监控另一个路径?是的。我想在这种情况下你需要保留List<MyFileWatcher>。如果不知道您的确切要求,这很难说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多