How do I write a Windows service that polls a directory for new files?
I made a window service, it basically reads a temporary file on my machine, reading it into the database and deleting it. I wrote the code for these actions in a method onStart
, so I need to restart the service again when I need it to work, but I really need the service to automatically recognize the creation of a file in my folder and then work on it.
So where should I place this particular code in my Windows service?
Since the FileSystemWatcher cannot notify you of all file system changes, I would recommend polling it using a Timer with a set interval. This approach is simpler for system resources too.
Internally, FSW buffers events received from the file system. If there are too many events happening at the same time, this buffer will overflow and you will start playing events. If the event handler code does not finish very quickly, or you are not using some kind of queuing mechanism, you will also start playing events. IMO this makes FSW a less than 100% reliable approach.
Below is an article that discusses FSW and performance in more detail .
System resource note: FSW relies on registering a callback with the OS file system. I have no metrics as to how much this is accounted for in system resources. My suggestion to use a timer while actively polling the system does not require such resources from the file system. You can try the filesystem every 30 seconds or every 5 minutes, depending on how quickly you need the files to be picked up.
Unless you really want real-time behavior that ...
source to share
If you are using a timer or FileSystemWatcher, both will fire events that you need to subscribe to. When the file is found, an event occurs. This starts a new thread that executes the event handler.
Therefore, in your OnStart method, you need to subscribe to the event. For this to work, your event handler must be a method that is somehow accessible from your OnStart ().
For recording I have been fired with FSW several times and do not use any more, I find it too unreliable. I wrote my own FileWatcher class that does what Peter Lillevold suggests and uses a timer to poll the directory again. Maybe a little rough, but it works. And you cannot beat that; -)
source to share
I recently created something that almost accurately describes. At first I thought I should use FileSystemWatcher. The problem is she only told me about things when they happened. If the service has been disabled and needs to be restarted for any reason, when it starts it will not trigger any events related to files that were generated during maintenance.
So, I stared at writing my own poller that will check files when the service starts and then use the FileSystemWatcher. Then I started thinking, why bother with FileSystemWatcher at all and contain 2 pieces of code? So I ditched it completely and just tried doing it all, because the app just needs to do what the player does in the startup process over and over again.
My application is a windows service. OnStart creates a thread that executes the polling function. This function is just a while loop with an end condition triggered by OnStop and it just checks every minute and walks through the directory. If he finds something he can handle, if he does not sleep for 1 minute. Pretty simple. The code is basic and easy to maintain.
Here's an example of my OnStart:
protected override void OnStart(string[] args)
{
WriteToLog("Starting...");
bThreadRun_m = true;
tMain_m = new Thread(new ThreadStart(RunThread));
tMain_m.Name = "Main Thread";
tMain_m.Start();
WriteToLog("Start Complete");
}
source to share