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.
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.
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.
Nope, filesystemwatchers run on their own thread.
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.
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.
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.
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.
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.
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()
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With