1

Hi developing friends!

Situation: I have a small service for some simple file exchange jobs to move files from one system to another and do some search and replace/unzip stuff. The service written in C# uses the FileSystemWatcher to check for new files in the folder. The Code:

private static void Main(string[] args)
{
    try
    {
        InitializeService();
    }
    catch (Exception ex)
    {

    }

    fsw = new FileSystemWatcher();
    fsw.Path = RootPath;
    //Watch only directories
    fsw.NotifyFilter = NotifyFilters.DirectoryName;

    //Add the event functions
    fsw.Created += FileSystemEvent;
    //fsw.Changed += FileSystemEvent;
    fsw.Error += OnError;

    //start the listener
    fsw.EnableRaisingEvents = true;

    Console.WriteLine("Started with path: " + RootPath);

    Console.ReadLine();
}

Problem description: The path for the filewatcher is on another server, so I'm connecting to a share. From time to time the filewatcher loses the connection to the directory (network issue, server reboot during maintenance window or what ever). If this happens the filewatcher does not reconnect to the server or throw an exception or any other indication that he's no longer connected. Just does nothing!

Question Is there anything I can do to check if the filewatcher has lost the connection? Because my workaround now is that I restart the server every night with a scheduled job and check first for existing files and process them before. But that's not what I think should be the idea if you use a filewatcher.

Many thanks

Andre
  • 662
  • 1
  • 8
  • 19
  • Have you already seen [this question](http://stackoverflow.com/questions/151804/system-io-filesystemwatcher-to-monitor-a-network-server-folder-performance-con) and its accepted answer? – dymanoid Aug 24 '15 at 12:40
  • in the accepted answer is written that a temporary network problem will fire the error event, which is not the case here. – Andre Aug 24 '15 at 12:58

2 Answers2

2

Perhaps the garbage collector removes the FileSystemWatcher instance.

Try GC.KeepAlive:

Console.ReadLine();
GC.KeepAlive(fsw);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Why? I still have a active reference in my service to the fsw – Andre Aug 24 '15 at 13:05
  • 1
    I found a half-dozen solutions scattered across the Internet, including some others on StackExchange sites, all of which were long & involved. This elegant solution fixed the issue for me in one line. Thank you. – SQLServerSteve Dec 07 '22 at 19:36
0

Have you tried to put all the FSW-stuff inside a method with a try-catch? When the method exits you could simply call it again in a while-loop like this (pseudo):

private static void Main(string[] args)
{
    while (true)
    {
        CallWatcher();
    }
}

private static void CallWatcher() 
{
    var watcher = new FileSystemWatcher();
    try
    {
        // do watcher stuff here
    }
    finally 
    {
        watcher.Dispose();
    }
}
Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
  • I give it a try. sadly the issue only is hard to test and it takes some day's to see if it works. – Andre Aug 25 '15 at 13:39