0

I have built a Windows Service which on start builds a list of file paths from a specified directory. I then do some processing on those files and delete them. My question is how often will the service build this list? Can we specify a time interval when this service would look into the directory and build the list?

OR

Is it better that I create a console application and assign it to a scheduler?

Jonathan Nixon
  • 4,940
  • 5
  • 39
  • 53
blue piranha
  • 3,706
  • 13
  • 57
  • 98
  • 2
    The service will build the list as often as you make it. You can add a `Timer` to the service to rebuild the list at an interval that you desire. It is not better to create a console application and assign it to a scheduler. It is not worse either. – Keith Payne Sep 26 '13 at 15:17
  • 2
    See `FileSystemWatcher` for an alternate approach. – Keith Payne Sep 26 '13 at 15:17
  • What's wrong with a FileSystemWatcher? – David Brabant Sep 26 '13 at 15:17
  • 1
    Yes, you can specify a time interval to do processing. A simple loop with a sleep delay in it will suffice for that. But without knowing what your requirements actually are, I don't know how anyone could offer more. – Carey Gregory Sep 26 '13 at 15:17
  • @KeithPayne A background process dumps about 3K files at a time. I do a DB processing of each file. Would not like to use a FileSystemWatcher which gets activated on seeing a single file. I would then need 3K DB connections. – blue piranha Sep 26 '13 at 15:21

2 Answers2

1

You can set a timer in the application and run your method whenever you like. See this question for a good answer.

Best Timer for using in a Windows service

Some other useful MSDN on building a service, including notes on OnStart and OnStop, which you might want to take advantage of.

ServiceBase

@Marius also makes a great point about the FileSystemWatcher which a great and often overlooked piece of .Net... FileSystemWatcher

Community
  • 1
  • 1
tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53
0

This is most likely a perfect use case for a FileSystemWatcher which "listens to the file system change notifications and raises events when a directory, or file in a directory, changes." By using it, you avoid having to manually schedule file or directory checks.

Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
  • Without knowing what the purpose of his application is, how do you know that real-time monitoring is a perfect solution? – Carey Gregory Sep 26 '13 at 15:19
  • @CareyGregory I don't, thus "most likely" and not "definitely". – Marius Schulz Sep 26 '13 at 15:20
  • @MariusSchulz A background process dumps about 3K files at a time. I do a DB processing of each file. Would not like to use a FileSystemWatcher which gets activated on seeing a single file. I would then need 3K DB connections. – blue piranha Sep 26 '13 at 15:33