Starting simple C# FileSystemWatcher Windows service quickly
c#, filesystemwatcher
Solution
You can avoid threading entirely. Just do basic setup in the `OnStart()` method. Part of that setup is setting up a timer to go off in a second or two. That timer can run on the current thread, but will happen after the service is idle.
This will solve the issue, and it's easier that writing thread-safe code.
Problem
I have a Windows service that I am writing in C#. Behind the scenes it a FileSystemWatcher. The FSW looks for new files and processes them accordingly. When my service starts, it also needs to process existing files. When I do this via a console app, everything works as expected. However, when I try to wrap this all in a Win service, my first issue was that the Win service would not start. It timed out because, in the even there are a lot of files to be processed initially, it took too long to process. Here is a portion of the code for my "watching" class: ``` public WatcherService() { _log.Debug("WatcherService instantiated."); _watcher = new FileSystemWatcher { Path = AppConfig.MonitorFolder, IncludeSubdirectories = true }; // we want the watching to start BEFORE we process existing files // because if we do it the other way, a file might get missed _watcher.Created += File_OnChanged; } public void StartWatching() { _log.Debug("WatcherService started."); // this kicks off the watching _watcher.EnableRaisingEvents = true; // process existing files ProcessExistingFiles(AppConfig.MonitorFolder); } ``` My workaround was to kick off the FSW "watching" and the processing of the initial files on a separate asynchronous thread, like this (in my Windows service code): ``` protected override void OnStart(string[] args) { _log.Debug("LoggingService starting."); // kick off the watcher on another thread so that the OnStart() returns faster; // otherwise it will hang if there are a lot of files that need to be processed immediately Task.Factory.StartNew(() => _watcher.StartWatching()).ContinueWith(t => { if (t.Status == TaskStatus.Faulted) { _log.Error("Logging service failed to start.", t.Exception.InnerException ?? t.Exception); } }); } ``` If I did not wrap that "StartWatching" method in the Task.Factory.StartNew(), the OnStart() timed out, understandably so. But now it seems my StartWatching() method is never called. I see "LoggingService starting" in my logs, but not "WatcherService started". (Edit: FYI I have tried Task.Run() as well, to no avail.) What up with that? I am sure I either don't understand what StartNew() is doing and/or there is a better to do what I am trying to accomplish. Thoughts? Thanks!