CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2011
    Posts
    54

    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;
    }

    }



    static void watcherDeleted(object sender, FileSystemEventArgs e)
    {
    anyFileDeleted = true;

    // 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);
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: filesystemwatcher in monitoring multiple directories - how to stop then start...

    Instead of storing a list of folders to monitor, store a list of FileSystemWatcher objects (or store both).

    When you want to stop watching, unhook the event handler, call Stop and remove the FileSystemWatcher from the list.

    Code:
    watcher.Deleted -= new FileSystemEventHandler(watcherDeleted);
    Instead of using a list to store the FileSystemWatcher objects, consider using a dictionary where the key is the full path to the file system folder.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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" );
    P.S. Be sure to add some error checking.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  6. #6
    Join Date
    Apr 2011
    Posts
    54

    Re: filesystemwatcher in monitoring multiple directories - how to stop then start...

    thanks... i'll try this and let you know..

  7. #7
    Join Date
    Apr 2011
    Posts
    54

    Re: filesystemwatcher in monitoring multiple directories - how to stop then start...

    PERFECTO!!! IT WORKED!!! Thanks all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured