0

I am wanting to create a Windows Service for the first time, but just have a question about the logic of it:

I want to create a service that every x seconds, poll's a directory, reads all the text files in this directory, inserts the details into the database and then archives this file.

How do I do this so that it runs every x seconds? (which x is defined in a configuration file).

Do I do a loop that says something like:

int interval = 5000;
while (TRUE) {
   //my logic
   Thread.Sleep(interval); //pause for 5 seconds
}

Is this the accepted method to achieve this?

Mark Hall
  • 52,990
  • 9
  • 92
  • 107
Lock
  • 5,334
  • 13
  • 60
  • 106
  • Remember to lock (no pun) files etc if the service runs more than 5 seconds. Alternatively look a FileSystemWatcher. – Thomas Aug 27 '12 at 05:50

2 Answers2

3

You can use FileSystemWatcher instead of checking every x seconds.You will be notified whenever a file or folder in the given folder is created,deleted or changed

Check Here from msdn.

Rasel
  • 16,321
  • 6
  • 41
  • 51
1

Alliteratively, you can you the FileSystemWatcher API. It trigger an event when the folder changes. This save your battery life if you are on laptop.

How can I monitor a Windows directory for changes?

Community
  • 1
  • 1
J-16 SDiZ
  • 25,723
  • 3
  • 63
  • 83