0

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.

user726720
  • 1,127
  • 7
  • 25
  • 59
  • 2
    You're creating a local variable `myWatcher` in `button9_Click` (so, `MyFileWatcher` will probably be garbage collected, yes.). What happends if you add that `MyFileWatcher` as a field instead? – default Sep 25 '14 at 06:45
  • possible duplicate of [FileSystemWatcher - only the change event once firing once?](http://stackoverflow.com/questions/1313942/filesystemwatcher-only-the-change-event-once-firing-once) – Renatas M. Sep 25 '14 at 06:45
  • But `myWatcher` is a local variable which could be collected. It doesn't implement `IDisposable` (which it should), but eventually the `FileSystemWatcher` would be collected too. – Dirk Sep 25 '14 at 06:46
  • @Dirk how do i stop that from happening – user726720 Sep 25 '14 at 06:47
  • @Reniuz: I did have a look at that link, but that doesn't satisfy my question – user726720 Sep 25 '14 at 06:49
  • By not making it a local variable. You could use a field of the class, either to store the `myWatcher` directly or in a list if you want to have multiple `MyFileWatchers`. – Dirk Sep 25 '14 at 06:49
  • @Dirk: Please refer to MyFileWatcher class, I have _watcher as a field for that class. I'm sorry my understanding here is a bit limited. Need more explanation – user726720 Sep 25 '14 at 06:52
  • 2
    @user726720 I'm not talking about the `_watcher` field, I'm talking about the `myWatcher` local variable of the `button9_Click` method. – Dirk Sep 25 '14 at 06:53
  • +1 to Dirk & Default. I was looking at the wrong variable. It now works. Kindly please put this up as an answer so I may accept it. THank you – user726720 Sep 25 '14 at 07:01
  • @Dirk: Just a quick question. Would I have declare separate fields for each instance of filesystemwatcher ? My take on this is yes. What's your take on this. – user726720 Sep 25 '14 at 07:27

1 Answers1

2
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.

default
  • 11,485
  • 9
  • 66
  • 102
  • Thanks I have done that. Would I have to declare myWatcher field for each instance. As I expect at least 7 instance's to run simultaneously. – user726720 Sep 25 '14 at 08:19
  • Do you mean if you want to monitor another path? Yes. I guess you would need to keep a `List` in that case. Its hard to say without knowing your exact requirements. – default Sep 25 '14 at 08:31