2

I have properly and made the EnableRaisingEvents to false, yet it raises events, here is how I'm doing it:

    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;

    }

Is there anything else that needs to be done, maybe I'm missing out here. Please advise. http://pastebin.com/yEaffBxP

T.S.
  • 18,195
  • 11
  • 58
  • 78
user726720
  • 1,127
  • 7
  • 25
  • 59
  • May be it raised event that happened before it detected that you said "Cancel monitoring"? – T.S. Jun 10 '14 at 04:28
  • This is still under testing so I have complete control while I'm monitoring it. So I'm sure there is no event raised. After I press the button and then try to raise the event, filesystemwatcher is still working – user726720 Jun 10 '14 at 04:34
  • once you click the button does it continue constantly monitoring or it only does one extra time? May be you press the button and re-wire your monitor? – T.S. Jun 10 '14 at 04:35
  • no I raise the event after monitoring to test if this is disabled or not, but it's still working – user726720 Jun 10 '14 at 04:41
  • I think, you just not getting that somewhere you have start monitoring without knowing it. May be we need to see your entire code on this – T.S. Jun 10 '14 at 04:44
  • 1
    Here you go : http://pastebin.com/yEaffBxP – user726720 Jun 10 '14 at 04:46
  • Don't make FileSystemWatcher variable static. – Ricky Jun 10 '14 at 04:54
  • 1
    First of all, and I see this already, you effectively have created 2 file watchers. One is here `private static FileSystemWatcher _watcher = new FileSystemWatcher();`, second - later in WatchFile(). Reading... – T.S. Jun 10 '14 at 04:56
  • @ricky: How will that help, I removed static yet I can't stop FSW from raising events – user726720 Jun 10 '14 at 04:58
  • 1
    its best practice to not use static variables otherwise you will most likely be stepping over data. It also logically makes no sense to store it in a static global variable, this also defeats encapsulation and true OO practices. – Ricky Jun 10 '14 at 05:03
  • @T.S. Yes that helped, I totally forgot about the second instance, I had put that a few days back for testing something, but it raised these issues today and couldn't catch the problem. Really thank you for noticing that. Kindly please put this as an answer so I can accept it. What a dumb fellow I am. Hat's off to you for noticing it. Thank you very much – user726720 Jun 10 '14 at 05:04
  • @ricky: good comment, I will take care of that in the future. Thank you – user726720 Jun 10 '14 at 05:05
  • Hey, glad to help. Enjoy, thanks for points. Don't beat yourself. Don't we all do it? – T.S. Jun 10 '14 at 05:09

2 Answers2

1

To stop the watcher set its EnableRaisingEvents property to False. If you've finished with it then Dispose it.

Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
  • can you show me an example, I have already set it to false but I'm calling dispose right after it. Where the dispose should be called – user726720 Jun 10 '14 at 04:40
1

I see this already, you effectively have created 2 file watchers. One is here

private static FileSystemWatcher _watcher = new FileSystemWatcher();

second - later in

WatchFile()

That should solve it

T.S.
  • 18,195
  • 11
  • 58
  • 78