private void StopWatcher(RecoveringFileSystemWatcher watcher) { if (watcher != null) { watcher.Dispose(); Trace.TraceInformation("FileWatcher stopped"); } }
private RecoveringFileSystemWatcher StartWatcher() { var watcher = new RecoveringFileSystemWatcher(AppDirectory.LibraryPath); watcher.All += _watcher_All; watcher.Error += _watcher_Error; //watcher.DirectoryMonitorInterval = TimeSpan.FromSeconds(10); //watcher.EventQueueCapacity = 1; watcher.EnableRaisingEvents = true; return(watcher); }
public static void StartWatchingFiles(bool log = true) { StopWatchingFiles(); StopCloudWatchTimer(); watcherVids = new List <RecoveringFileSystemWatcher>(); foreach (SVR_ImportFolder share in Repo.Instance.ImportFolder.GetAll()) { try { if (share.FolderIsWatched && log) { logger.Info($"Watching ImportFolder: {share.ImportFolderName} || {share.ImportFolderLocation}"); } if (share.CloudID == null && Directory.Exists(share.ImportFolderLocation) && share.FolderIsWatched) { if (log) { logger.Info($"Parsed ImportFolderLocation: {share.ImportFolderLocation}"); } RecoveringFileSystemWatcher fsw = new RecoveringFileSystemWatcher { Path = share.ImportFolderLocation }; // Handle all type of events not just created ones fsw.Created += Fsw_CreateHandler; fsw.Renamed += Fsw_RenameHandler; // Commented out buffer size as it breaks on UNC paths or mapped drives //fsw.InternalBufferSize = 81920; fsw.IncludeSubdirectories = true; fsw.EnableRaisingEvents = true; watcherVids.Add(fsw); } else if (!share.FolderIsWatched) { if (log) { logger.Info("ImportFolder found but not watching: {0} || {1}", share.ImportFolderName, share.ImportFolderLocation); } } } catch (Exception ex) { logger.Error(ex, ex.ToString()); } } StartCloudWatchTimer(); }
private RecoveringFileSystemWatcher CreateFileSystemWatcher(string path) { var watcher = new RecoveringFileSystemWatcher() { Path = path, IncludeSubdirectories = true }; watcher.Created += ProcessFileSystemEvent; watcher.Existed += ProcessFileSystemEvent; watcher.Error += OnError; return(watcher); }
public void RunRecoveringWatcher() { //var TestPath = Path.GetTempPath()+"\\test"; var TestPath = Path.GetTempPath(); var testFilePath = Path.Combine(TestPath, testFile); Console.WriteLine("Will auto-detect unavailability of watched directory"); Console.WriteLine(" - Windows timeout accessing network shares: ~110 sec on start, ~45 sec while watching."); using (var watcher = new RecoveringFileSystemWatcher(TestPath, "*.txt")) { //watcher.All += Watcher_All; //watcher.All += (_, e) => { Console.WriteLine("{0} {1}", e.ChangeType, e.Name); }; //watcher.Error += (_, e) => { Console.WriteLine("Error: "+ e.Error.Message); }; watcher.Created += (_, e) => { Console.WriteLine("{0} {1}", e.ChangeType, e.Name); created++; }; watcher.Existed += Watcher_Existed; //watcher.Created += Watcher_Created; //watcher.Error += (_, e) => // { WriteLineInColor($"Suppressing auto handling of error {e.Error.Message}", ConsoleColor.Red); // //... // e.Handled = true; // }; watcher.DirectoryMonitorInterval = TimeSpan.FromSeconds(2); watcher.EventQueueCapacity = 1; watcher.EnableRaisingEvents = true; //watcher.NotifyFilter = NotifyFilters.Attributes | // NotifyFilters.CreationTime | // NotifyFilters.FileName | // NotifyFilters.LastAccess | // NotifyFilters.LastWrite | // NotifyFilters.Size | // NotifyFilters.Security; //PromptUser("Processing..."); if (File.Exists(testFilePath)) { File.Delete(testFilePath); } var deadmanswitch = 0; do { Thread.Sleep(100); deadmanswitch++; File.WriteAllText(testFilePath, "Hello world!"); }while (created == 0 && deadmanswitch < 100); Assert.IsTrue(created > 0); Console.WriteLine("Existed! " + existed); } }
public void Start() { var asm = new Assembly[] { typeof(IOCAttribute).Assembly, typeof(IGraphBuilder).Assembly }; UnitySingleton.Container.RegisterTypesFromAssemblies(asm); Trace.TraceInformation("Register types from assemblies"); var start = new StartOptions(); start.Urls.Add("http://" + App.Config["endpoint"].ToStringValue("+") + ":" + App.Config["port"].ToInteger(12602) + "/"); start.Urls.Add("https://" + App.Config["endpoint"].ToStringValue("+") + ":" + App.Config["sslport"].ToInteger(12702) + "/"); _webApp = WebApp.Start <StartOwin>(start); Trace.Listeners.RemoveAt(Trace.Listeners.Count - 1); _watcher = StartWatcher(); }
static void RunRecoveringWatcher() { Console.WriteLine("Will auto-detect unavailability of watched directory"); Console.WriteLine(" - Windows timeout accessing network shares: ~110 sec on start, ~45 sec while watching."); using (var watcher = new RecoveringFileSystemWatcher(TestPath)) { watcher.All += (_, e) => { WriteLineInColor($"{e.ChangeType} {e.Name}", ConsoleColor.Yellow); }; watcher.Error += (_, e) => { WriteLineInColor(e.Error.Message, ConsoleColor.Red); }; //watcher.Error += (_, e) => // { WriteLineInColor($"Suppressing auto handling of error {e.Error.Message}", ConsoleColor.Red); // //... // e.Handled = true; // }; //watcher.DirectoryMonitorInterval = TimeSpan.FromSeconds(10); //watcher.EventQueueCapacity = 1; watcher.EnableRaisingEvents = true; PromptUser("Processing..."); Console.WriteLine("Stopping..."); } PromptUser("Stopped."); }
private static void StartFileSystemWatcher() { fileSystemWatchers = new Dictionary <string, RecoveringFileSystemWatcher>(); for (var i = 0; i < 26; i++) { var driveLetter = (char)(i + 'A'); // Source: https://petermeinl.wordpress.com/2015/05/18/tamed-filesystemwatcher/ var watcher = new RecoveringFileSystemWatcher(); watcher.IncludeSubdirectories = true; watcher.OrderByOldestFirst = true; // To enable LastAccess filter: fsutil behavior set DisableLastAccess 0 (and reboot?) watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size;// | NotifyFilters.LastAccess | NotifyFilters.Security | NotifyFilters.Attributes; watcher.SetIgnoreEventCallback <SuncatLog>(IgnoreEventCallback); watcher.Created += FileSystemEventHandler; watcher.Changed += FileSystemEventHandler; watcher.Deleted += FileSystemEventHandler; watcher.Renamed += FileSystemEventHandler; fileSystemWatchers.Add(driveLetter.ToString(), watcher); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Task backgroundServiceStop = Task.Delay(TimeSpan.FromMilliseconds(-1), stoppingToken); while (!stoppingToken.IsCancellationRequested) { try { using (FileSystemWatcher = CreateFileSystemWatcher(DirectoryToWatch)) { logger.LogInformation("starting to watch {directoryToWatch}", DirectoryToWatch); FileSystemWatcher.EnableRaisingEvents = true; try { await backgroundServiceStop; } catch (TaskCanceledException) { logger.LogInformation("background service has been cancelled"); } finally { logger.LogInformation("stopping to watch {directoryToWatch}", DirectoryToWatch); FileSystemWatcher.EnableRaisingEvents = false; } } } catch (Exception e) { logger.LogError(e, "Some error"); } var waitBeforeRetry = Task.Delay(TimeSpan.FromSeconds(5)); await Task.WhenAny(backgroundServiceStop, waitBeforeRetry); } }
public void Continue() { _watcher = StartWatcher(); }