Search code examples
c#.netc#-4.0filesystemwatcher

FileSystemWatcher does not fire after sometime


I have the following code that i use to monitor a directory for text files, the directory gets new files twice a day, the code works fine for sometime but after that it stops firing OnCreated event...

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = @"c:\users\documents\";

    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    watcher.Filter = "*.txt";

    // Add event handlers.
    watcher.Created += new FileSystemEventHandler(OnCreated);

    // Begin watching.
    watcher.EnableRaisingEvents = true;

    // Wait for the user to quit the program.
    Console.WriteLine("Press \'q\' to quit the sample.");
    while(Console.Read()!='q');
}

private static void OnCreated(object source, FileSystemEventArgs e)
{
   Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}

Cannot figure out the issue.

Also, I would like to know a foolproof alternative to this (if any), since I am not finding this reliable..


Solution

  • The Problem with this was that the reference to the FILESYSTEMWATCHER was being collected by the GC and hence after sometime the FILEWATCHER had a null reference leading to events not getting raised.

    Solution :-

    private static FileSystemWatcher watcher;
    public static void Run()
    {
    watcher = new FileSystemWatcher();
    ...
    GC.KeepAlive(watcher);  
    }
    

    Just keeping a reference of the watcher in the outer scope as suggested did not solve the problem. I had be explicitly specify that GC should not collect the FileWatcher object.