filesystemwatcher in monitoring multiple directories - how to stop then start...
I have this program that monitors multiple directories (watch for files when deleted, then get their timestamp).. I was able to do this 'dynamically' where it can be 'x' number of directories (at the time the program starts)...
The problem is that when I call StopWatching, and then I say delete a file from any of the directories, FileSystemWatcher seems to still be "watching". I wanted FileSystemWatcher to be ON, then OFF, then ON again at certain parts of the program.. for now, I can't make it to where it's OFF.
Thanks in advance
I have this method:
static FileSystemWatcher watcher = new FileSystemWatcher();
static void Main(string[] args)
{
.
.
<then I call the method StartWatching here>
.
.
<then some processes and other methods called here, etc>
.
.
<then somewhere in the code i would need Filesystemwatcher to stop watching the directories>
<I call this: StopWatching>
.
.
<then somewhere in the code (again), I would need Filesystemwatcher to start watching>
.
.
}
Here are the methods:
StartWatching
static public void StartWatching()
{
string sourceFolderToWatch = "";
for (int i = 0; i < sourceFolderList.Count; i++)
{
sourceFolderToWatch = (string)sourceFolderList[i];
watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = true;
watcher.Deleted += new FileSystemEventHandler(watcherDeleted);
}
}
static public void StopWatching()
{
string sourceFolderToWatch = "";
for (int i = 0; i < sourceFolderList.Count; i++)
{
sourceFolderToWatch = (string)sourceFolderList[i];
watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = false;
}
// the value for deleteTime only changes if theres a delete that happened
referenceTime = DateTime.Now;
Console.WriteLine("******FILE DELETED******");
Console.WriteLine("This file was deleted: " + e.Name);
Console.WriteLine(referenceTime);
}
Re: filesystemwatcher in monitoring multiple directories - how to stop then start...
Arjay's solution is the correct one. I think the core of the issue is that you have (see bolded):
Code:
static public void StopWatching()
{
string sourceFolderToWatch = "";
for (int i = 0; i < sourceFolderList.Count; i++)
{
sourceFolderToWatch = (string)sourceFolderList[i];
watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = false;
}
}
That is, you are disabling the EnableRaisingEvents on a new FileSystemWatcher object that you just created instead of setting the property on the FileSystemWatcher instance you previously created in StartWatching(). If you had a static array of active FileSystemWatchers that was a class variable (i.e. 'global' scope), you could - I think - just set WatcherArray[whichever].EnableRaisingEvents = false on the one you wanted to stop without needing to do the complexity of unhooking the handler and all as Arjay suggests. (Um, feel free to disagree if this is incorrect, Arjay or others)
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Re: filesystemwatcher in monitoring multiple directories - how to stop then start...
I agree with Bio and you can probably get away with setting the EnableRaisingEvents property to false. There might be some pending events that will fire after you've set the property to false so that's why I suggested unhooking the event.
In terms of storing items in an array. This is convenient if you want to manage all the items in the array, but it isn't so convenient to access a single item. This is why I suggested using a dictionary.
Code:
Dictionary< String, FileSystemWatcher > _watchers = new Dictionary< String, FileSystemWatcher>( );
for (int i = 0; i < sourceFolderList.Count; i++)
{
var sourceFolderToWatch = (string)sourceFolderList[i];
var watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = true;
watcher.Deleted += new FileSystemEventHandler(watcherDeleted);
// add the watcher to the dictionary
_watchers.Add( sourceFolderToWatch, watcher );
}
Code:
//Access a single watcher (by folder path)
_watchers[ "folder path" ].EnableRaisingEvents = false;
// Delete a watcher
_watchers.Delete( "folder path" );
Re: filesystemwatcher in monitoring multiple directories - how to stop then start...
Thanks for the clarification. Yeah, Dictionary is definitely the way to go.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks