Ejemplo n.º 1
0
        private void CleanupFileSystemWatcher()
        {
            if (_fileSystemWatcher is null)
            {
                return;
            }

            UnsubscribeFromEvents();

            _fileSystemWatcher.Dispose();
            _fileSystemWatcher = null;
        }
        public override void OnDelete(object sender, FileSystemEventArgs e)
        {
            base.OnDelete(sender, e);

            lock (lk)
            {
                IFileSystemWatcherWrapper childWatch;
                if (!watches.TryGetValue(e.FullPath, out childWatch))
                {
                    return;
                }

                watches.Remove(e.FullPath);

                watch.RemoveListener(this);
                watch.Dispose();
            }

            Logger.Debug("Child Directory Deleted: {0}", e.FullPath);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// @see: https://www.codeguru.com/dotnet/filesystemwatcher%EF%BF%BDwhy-does-it-stop-working/
        /// </summary>
        internal bool Retry(IFileSystemWatcherWrapper fileSystemWatcherWrapper, int numberOfTries = 20, int milliSecondsTimeout = 5000)
        {
            _webLogger.LogInformation("[DiskWatcher] next retry " +
                                      $"{DateTimeDebug()}");
            var path = _fileSystemWatcherWrapper.Path;

            _fileSystemWatcherWrapper.Dispose();
            _fileSystemWatcherWrapper = fileSystemWatcherWrapper;

            var i = 0;

            while (!_fileSystemWatcherWrapper.EnableRaisingEvents && i < numberOfTries)
            {
                try
                {
                    // This will throw an error at the
                    // watcher.NotifyFilter line if it can't get the path.
                    Watcher(path);
                    if (_fileSystemWatcherWrapper.EnableRaisingEvents)
                    {
                        _webLogger.LogInformation("[DiskWatcher] I'm Back!");
                    }
                    return(true);
                }
                catch
                {
                    _webLogger.LogInformation($"[DiskWatcher] next retry {i} - wait for {milliSecondsTimeout}ms");
                    // Sleep for a bit; otherwise, it takes a bit of
                    // processor time
                    System.Threading.Thread.Sleep(milliSecondsTimeout);
                    i++;
                }
            }
            _webLogger.LogError($"[DiskWatcher] Failed after {i} times - so stop trying");
            return(false);
        }
 public override void Dispose()
 {
     base.Dispose();
     watch?.Dispose();
     watch = null;
 }