private void DirectoryChangeWatcherOnError(Exception exception)
        {
            _taskExecutor.ExecuteAsync(token => {
                Logger.LogInfo("FileSystemSnapshotManager: Directory watcher error received, entering pause mode");
                // Note: No need to stop the directory watcher, it paused itself
                _isPaused = true;

                // Ingore all changes
                _pathsChangedQueue.DequeueAll();
                _longRunningFileSystemTaskQueue.CancelAll();
                OnFileSystemWatchPaused(new FileSystemWatchPausedEventArgs {
                    IsError = true
                });
            });
        }
        private void PollingThreadOnPolling(object sender, EventArgs eventArgs)
        {
            lock (_stateLock) {
                _state = _state.OnPolling();
                _state.OnStateActive();
            }

            var actions = _watcherEventQueue.DequeueAll();

            if (actions.Count > 0)
            {
                Logger.LogDebug("DirectectoryChangeWatcher: Processing {0} file change event(s)", actions.Count);
                foreach (var action in actions)
                {
                    Logger.WrapActionInvocation(() => {
                        action();
                    });
                }
            }
        }
        public IList <FileRegistrationEntry> DequeueAll()
        {
            var allEntries = _queue.DequeueAll();

            if (allEntries.Count == 0)
            {
                return(allEntries);
            }

            // Merge entries into a shorter list
            var singleEntries = new Dictionary <FullPath, FileRegistrationKind>();

            foreach (var entry in allEntries)
            {
                FileRegistrationKind previousKind;
                if (singleEntries.TryGetValue(entry.Path, out previousKind))
                {
                    // Path has been (un)registered, merge both kinds together
                    switch (previousKind)
                    {
                    case FileRegistrationKind.Register:
                        switch (entry.Kind)
                        {
                        case FileRegistrationKind.Register:
                            // Register + Register = leave as is
                            break;

                        case FileRegistrationKind.Unregister:
                            // Register + Unregister = remove entry
                            singleEntries.Remove(entry.Path);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        break;

                    case FileRegistrationKind.Unregister:
                        switch (entry.Kind)
                        {
                        case FileRegistrationKind.Register:
                            // Unregister + Register = remove entry
                            singleEntries.Remove(entry.Path);
                            break;

                        case FileRegistrationKind.Unregister:
                            // Unregister + Unregister = leave as is
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    // Path is not registered, simply add the entry.
                    singleEntries.Add(entry.Path, entry.Kind);
                }
            }
            return(singleEntries.Select(x => new FileRegistrationEntry(x.Key, x.Value)).ToList());
        }