Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher remove event handler

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.

like image 819
John Soer Avatar asked Feb 04 '10 05:02

John Soer


People also ask

How to stop a File System watcher?

To stop the watcher from reporting changes then set its EnableRaisingEvents property to False. If you've finished with it then Dispose it. Your problem is that you haven't retained a reference to any of the FileSystemWatchers you've created.

What is FileSystemWatcher in C#?

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

Is FileSystemWatcher multithreaded?

Nope, filesystemwatchers run on their own thread.

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.

How do I use the FileSystemWatcher with PowerShell?

Please refer to the Microsoft’s reference documentation of the FileSystemWatcher class for the details. The watcher now sends notifications to PowerShell’s engine event queue using the source identifier “MyEvent”. You can consume the event by registering an event handler for the same source identifier.

What are the event handler variables available in start-FileSystemWatcher?

All event handler script blocks you supply to Start-FileSystemWatcher have access to the following variables: $e - the argument to the original FileSystemWatcher event handler. For all events: $eventArgs - the event that was returned from Wait-Event This is set up by calling the PowerShell DoAction function for every event handler.

Why does the Order of the deleted event change in FileSystemWatcher?

The order in which the Deleted event is raised in relation to the other FileSystemWatcher events may change when the SynchronizingObject property is not null.

How do I associate a file system event with an event handler?

When you create a FileSystemEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.


1 Answers

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()...

like image 106
Mitch Wheat Avatar answered Oct 18 '22 07:10

Mitch Wheat