I have a service that opens multiple watchers to watch multiple folders. After watching folders for a certain amount of time I get the "The network bios command limit has been reached".
As I read on here this is caused by having more long term requests than allowed.
I believe this occurs due to the below error handling code i have, which is triggered by the watchers error event. This starts a new instance of the watcher by calling the WatchFile method again. I believe this leaves the old now defunct watcher running and starts a new watcher but i am afraid stopping the watcher will either prevent it from starting it up again or will stop all instances based on the watcher.
Or am I wrong and is the error dependent on the amount of changes? This would cause 100 files to drop in at the same time to cause this error.
I was thinking of stopping and starting the service whenever I run in this error but this would not solve the problem itself but just hide it. Is there a better solution to it?
private static void watcherError(String directory, Boolean intray, ErrorEventArgs e, FileSystemWatcher watcher)
{
Exception watchException = e.GetException();
EventLog.WriteEntry("WhiteFileMover", String.Concat("error gedetecteerd, watcher werd herstart - ", watchException.Message), EventLogEntryType.Information);
watcher = new FileSystemWatcher();
while (!watcher.EnableRaisingEvents)
{
try
{
// This will throw an error at the
// watcher.NotifyFilter line if it can't get the path.
WatchFile(directory, intray);
}
catch(Exception exp)
{
// Sleep for a bit; otherwise, it takes a bit of
// processor time
EventLog.WriteEntry("WhiteFileMover", String.Concat("Failed to restart watcher, retrying in 5 seconds - ", exp.Message), EventLogEntryType.Warning);
System.Threading.Thread.Sleep(5000);
}
}
}
Look at this line:
watcher = new FileSystemWatcher();
You passed in a FileSystemWatcher variable, but completely ignored the passed value. Instead, you created a new instance. Not only that, but you fail to correctly dispose the instance. My guess is that you have a bunch of old FileSystemWatcher objects hanging around waiting to be collected. Each of those will hold on to some real file system resources from the operating system. Over time, you run out of available file handles.