What is the best practice for recovering from a FileSystemWatcher error?

After the FileSystemWatcher.Error event was raised, I do not know what to do next. An exception may be [relatively] secondary, for example

too many changes at once in the directory

which does not affect the process of viewing the observer, but can also be a big problem, such as a deleted directory being watched, in which case the observer no longer works.

My question is how best to handle the Error event?

+6
source share
2 answers

Depends on the error?

  • If this is too much data, because the buffer was full (many changes), makes a list directory and captures the changes that you are after.
  • If this is too much data because you are not processing FileSystemWatcher events fast enough, make sure that you process it efficiently.
  • A remote directory can do nothing but delete FileSystemWatcher, or perhaps look at the parent to recreate that directory name again.
+2
source

I would just get the internal type of exception and then decide what to do with the error (reboot or crash).

So

 myWatcher.Error += new ErrorEventHandler(OnError); 

Followde by

 private static void OnError(object source, ErrorEventArgs e) { if (e.GetException().GetType() == typeof(InternalBufferOverflowException)) { // This can happen if Windows is reporting many file system events quickly // and internal buffer of the FileSystemWatcher is not large enough to handle this // rate of events. The InternalBufferOverflowException error informs the application // that some of the file system events are being lost. Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message)); } } 
+1
source

Source: https://habr.com/ru/post/921384/


All Articles