FileSystemWatcher hell... stops watching after event

I created a FileSystemWatcher object to watch for newly created files. Once a file is created, the FileCreated() method is called through the FileWatcher event handler. Problem is, after this happens, FileWatcher doesnt seem to listen anymore. I have to create a new FileWatcher every time the event is fired and the method completes. Below is my code. This is a seperate class from the Main class. From the Main, I call this class to set up and create the FileWatcher. I have tried to put the new FileSystemWatcher() in the MAIN class, but it stil quits listening after event. I also caled this watcher.WaitForChanged(WatcherChangeTypes.Created); after the FileCreated() method, but still nothing ... any help Thanks.

namespace FileWatcherConsole

{

public class DirectoryWatcher

{

protected static FileSystemWatcher watcher = new FileSystemWatcher();

public static void XmlWatcher()

{

watcher.Path = WatcherDir;

watcher.Filter = "*.xml";

watcher.NotifyFilter = NotifyFilters.FileName |

NotifyFilters.Attributes |

NotifyFilters.LastAccess |

NotifyFilters.LastWrite |

NotifyFilters.Security |

NotifyFilters.Size;

watcher.Created += new FileSystemEventHandler(FileCreated);

watcher.IncludeSubdirectories = true;

watcher.EnableRaisingEvents = true;

watcher.WaitForChanged(WatcherChangeTypes.Created);

}

}



Answer this question

FileSystemWatcher hell... stops watching after event

  • Zinser

    I removed the WaitForChanged, and added a Console.ReadLine();

    Same thing is happening. It fires the even for the first fire, then just seems to stop listening. My console app is still open and running, it just doesnt seem to do anything beyond the first file creation. Here is my code with the Main. Thanks for the help.

    class FileWatcherMain

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main(string[] args)

    {

    DirectoryWatcher who = new DirectoryWatcher();

    who.XmlWatcher();

    string hell = Console.ReadLine();

    }

    }

    public class DirectoryWatcher

    {

    FileSystemWatcher watcher = null;

    // Defines the directories for the DirectoryWatcher Object

    protected static string WatcherDir = ConfigurationManager.AppSettings"DirectoryWatcherCurrentDir"].ToString();

    public void XmlWatcher()

    {

    this.watcher = new FileSystemWatcher();

    watcher.Path = WatcherDir;

    watcher.Filter = "*.xml";

    watcher.NotifyFilter = NotifyFilters.FileName |

    NotifyFilters.Attributes |

    NotifyFilters.LastAccess |

    NotifyFilters.LastWrite |

    NotifyFilters.Security |

    NotifyFilters.Size;watcher.Created += new FileSystemEventHandler(FileCreated);

    watcher.IncludeSubdirectories = true;

    watcher.EnableRaisingEvents = true;

    }

    public void FileCreated(object source, FileSystemEventArgs e)

    {

    Console.WriteLine("HEY IT WORKS!!!****************");

    string hef = Console.ReadLine();

    }


  • brewdos

    This forum is for C# language issues only. For future reference, please use the .NET base classes forum for this kind of question.
  • Nicola Tuveri

    No, it is not a C# language question. It's a .NET framework question. There are many "dotnet" related forums and newsgroups you can peruse. Check out http://msdn.microsoft.com/newsgroups, and go to .NET Development.


  • Diogo Traldi de Oliveira

    The method that is fired upon the creation of a file is FileCreated().

    The last thing this method does is : watcher.WaitForChanged(WatcherChangeTypes.Created);

    The console app remains running. It just does not respond to the creation of subsequent files.


  • BGood

    Why are you both listening for the Created event and using WaitForChanged WaitForChanged adds an internal event listener for the Created event and blocks the main thread until the event has been raised by the file system watcher thread. Missed that when I glanced over the docs before answering the first time, sorry.

    So what's happening in your case I think is that when you call WaitForChanged from your Created event handler you block the file system watcher thread, which is not the same as your main application thread, so that it cannot handle any more file creations.


  • madhur

    What does you rmain method do after it has called XmlWatcher() FileSystemWatcher.WaitForChanged will return the first time a file is created, then your XmlWatcher method will return. So if your main method ends after its call to XmlWatcher() you'll have to add some code that keeps your console application alive.

  • Dmitriy Velichkin

    Thanks for all the help. Removing the second Console.realine did the trick. Is this not a c# question
  • SteveK1111

    Just remove the Console.ReadLine() call in your event handler and the code will work just fine.

    The console streams are synchronized, so when your main thread call Console.ReadLine it will take the lock for the standard input stream and the file system watcher thread will then wait on its call to ReadLine. But since your console application will exit when you press enter and cause the first Console.ReadLine call to return you'll only ever see one file creation.

  • FileSystemWatcher hell... stops watching after event