Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Timers;
- using Topshelf;
- namespace TopShelfCustomArgs
- {
- public class FolderProcessor
- {
- readonly Timer _timer;
- public FolderProcessor(string path, int frequency)
- {
- _timer = new Timer(frequency * 1000) { AutoReset = true };
- _timer.Elapsed += (sender, eventArgs) =>
- {
- string[] fileEntries = Directory.GetFiles(path);
- foreach (var fileEntry in fileEntries)
- Console.WriteLine($"At {DateTime.Now}: {fileEntry}");
- };
- }
- public void Start() { _timer.Start(); }
- public void Stop() { _timer.Stop(); }
- }
- public class Program
- {
- public static void Main()
- {
- string path = string.Empty;
- int frequency = 10;
- HostFactory.Run(x =>
- {
- x.Service<FolderProcessor>(s =>
- {
- s.ConstructUsing(name => new FolderProcessor(path, frequency));
- s.WhenStarted(tc => tc.Start());
- s.WhenStopped(tc => tc.Stop());
- });
- x.RunAsLocalSystem();
- x.SetDescription("Topshelf Host");
- x.SetDisplayName("Folder Processor");
- x.SetServiceName("Folder Processor");
- x.AddCommandLineDefinition("path", v => path = v);
- x.AddCommandLineDefinition("frequency", v => frequency = Int32.Parse(v));
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement