简体   繁体   中英

FilesystemWatcher firing events only once

I'm buidling an c# app that would have separate FilesystemWatcher instances to monitor multiple folders and update it's associated listboxes.

   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;
    }

This being fired from:

 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);

        }
    }

Now when this is fired. It works the first time but it doesn't pick up the second time. I feel like the _watcher instance is being garbage collected. But something I don't understand is it's declared as global within the MyFileWatcher class.

MyFileWatcher myWatcher = new MyFileWatcher(textBox1, listBox1, destfolder, this);

Since this is a local variable and MyFileWatcher owns the FileSystemWatcher it will be garbagecollected yes.

It should work fine if you for instance add the myWatcher as a field instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM