private void QueueInitialFiles(FileSystemEventQueue queue) { _logger.QueuingInitialFiles(); foreach (var file in Directory.GetFiles(_configuration.DirectoryToMonitor, _configuration.DirectoryFileFilter, SearchOption.TopDirectoryOnly)) { queue.Enqueue(new FileSystemEventArgs(WatcherChangeTypes.All, _configuration.DirectoryToMonitor, Path.GetFileName(file))); } }
private void Initialize(FileSystemEventQueue queue, FileSystemWatcher watcher) { var initializationTasks = new[] { Task.Run(() => InitializeWatcher(queue, watcher)), Task.Run(() => QueueInitialFiles(queue)) }; Task.WaitAll(initializationTasks); _isInitializedEvent.Set(); }
private void InitializeWatcher(FileSystemEventQueue queue, FileSystemWatcher watcher) { _logger.Initializing <FileSystemEventCollection>(); watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite; watcher.Created += (s, e) => queue.Enqueue(e); watcher.Changed += (s, e) => queue.Enqueue(e); watcher.Deleted += (s, e) => queue.Enqueue(e); watcher.Renamed += (s, e) => queue.Enqueue(e); watcher.EnableRaisingEvents = true; }
/// <inheritdoc/> /// <summary> /// Iterates over the collection of <see cref="FileSystemEventArgs"/> awaiting any new ones. /// This is long running and will block while waiting for the next file system event /// </summary> /// <remarks> /// On initial creation of collection will create an event for all files currently in /// monitored directory. /// </remarks> /// <returns> Non duplicate <see cref="FileSystemEventArgs"/> </returns> public IEnumerator <FileSystemEventArgs> GetEnumerator() { if (!_cancellationToken.IsCancellationRequested) { using (var watcher = new FileSystemWatcher(_configuration.DirectoryToMonitor, _configuration.DirectoryFileFilter)) using (var queue = new FileSystemEventQueue(_configuration.DuplicateEventDelayWindow.TotalMilliseconds, _logger)) { Initialize(queue, watcher); if (!_cancellationToken.IsCancellationRequested) { while (queue.TryDequeue(out var fileSystemEventArgs, _cancellationToken)) { yield return(fileSystemEventArgs); } } } } _logger.CancellationRequested(); }