Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Timers;
  4. using Topshelf;
  5.  
  6. namespace TopShelfCustomArgs
  7. {
  8. public class FolderProcessor
  9. {
  10. readonly Timer _timer;
  11. public FolderProcessor(string path, int frequency)
  12. {
  13. _timer = new Timer(frequency * 1000) { AutoReset = true };
  14. _timer.Elapsed += (sender, eventArgs) =>
  15. {
  16. string[] fileEntries = Directory.GetFiles(path);
  17. foreach (var fileEntry in fileEntries)
  18. Console.WriteLine($"At {DateTime.Now}: {fileEntry}");
  19. };
  20.  
  21.  
  22. }
  23. public void Start() { _timer.Start(); }
  24. public void Stop() { _timer.Stop(); }
  25. }
  26.  
  27. public class Program
  28. {
  29. public static void Main()
  30. {
  31. string path = string.Empty;
  32. int frequency = 10;
  33.  
  34.  
  35. HostFactory.Run(x =>
  36. {
  37. x.Service<FolderProcessor>(s =>
  38. {
  39. s.ConstructUsing(name => new FolderProcessor(path, frequency));
  40. s.WhenStarted(tc => tc.Start());
  41. s.WhenStopped(tc => tc.Stop());
  42. });
  43. x.RunAsLocalSystem();
  44.  
  45. x.SetDescription("Topshelf Host");
  46. x.SetDisplayName("Folder Processor");
  47. x.SetServiceName("Folder Processor");
  48.  
  49.  
  50. x.AddCommandLineDefinition("path", v => path = v);
  51. x.AddCommandLineDefinition("frequency", v => frequency = Int32.Parse(v));
  52.  
  53. });
  54.  
  55. }
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement