BackgroundService.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using MediaBrowser.Model.Logging;
  2. using System.Linq;
  3. using System.ServiceProcess;
  4. namespace MediaBrowser.ServerApplication
  5. {
  6. /// <summary>
  7. /// Class BackgroundService
  8. /// </summary>
  9. public class BackgroundService : ServiceBase
  10. {
  11. public static string Name = "Emby";
  12. public static string DisplayName = "Emby Server";
  13. public static string GetExistingServiceName()
  14. {
  15. try
  16. {
  17. if (ServiceController.GetServices().Any(s => s.ServiceName == "MediaBrowser"))
  18. {
  19. return "MediaBrowser";
  20. }
  21. }
  22. catch
  23. {
  24. return "MediaBrowser";
  25. }
  26. return Name;
  27. }
  28. private readonly ILogger _logger;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="BackgroundService"/> class.
  31. /// </summary>
  32. public BackgroundService(ILogger logger)
  33. {
  34. _logger = logger;
  35. CanPauseAndContinue = false;
  36. CanStop = true;
  37. ServiceName = GetExistingServiceName();
  38. }
  39. /// <summary>
  40. /// When implemented in a derived class, executes when a Stop command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service stops running.
  41. /// </summary>
  42. protected override void OnStop()
  43. {
  44. _logger.Info("Stop command received");
  45. base.OnStop();
  46. }
  47. }
  48. }