views:

160

answers:

1

For some reason I can not remove an event handler from the 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 call start I start receiving the change events, but when I call stop I am expecting the events to stop but they are still being raised.

A: 

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 convinced that's the best place for the Dispose()...

Mitch Wheat
I added in the "EnableRaisingEvents = false" the event is still being raised. There are ways around it by using a flag to ignore the event, however my big concern is that if the events are not being cleaned up and over time this could be bad.
John Soer
Never mind I was an idiot - I never called "Stop".Everything is working fine now.
John Soer