FileSystemWatcher removes the event handler

For some reason, I cannot remove the event handler from FileSystemWatcher.

This is what I have

void Start() { ivFileSystemWatcher = new FileSystemWatcher(); ivFileSystemWatcher.Changed += new FileSystemEventHandler(ivFileSystemWatcher_Changed); } void Stop() { ivFileSystemWatcher.Changed -= new FileSystemEventHandler(ivFileSystemWatcher_Changed); ivFileSystemWatcher.Dispose(); } 

When I start a call, I start to receive change events, but when I call a stop, I expect the events to stop, but they are still raising.

+4
source share
1 answer

Have you tried setting EnableRaisingEvents to false :

 void Stop() { ivFileSystemWatcher.EnableRaisingEvents = false; ivFileSystemWatcher.Changed -= new FileSystemEventHandler(ivFileSystemWatcher_Changed); ivFileSystemWatcher.Dispose(); } 

Without seeing the rest of your code, I'm not sure what the best place for Dispose() ...

+6
source

Source: https://habr.com/ru/post/1300268/


All Articles