HostLifecycleService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.ComponentModel;
  5. using System.Threading;
  6. namespace System.Reactive.PlatformServices
  7. {
  8. /// <summary>
  9. /// (Infrastructure) Provides access to the host's lifecycle management services.
  10. /// </summary>
  11. [EditorBrowsable(EditorBrowsableState.Never)]
  12. public static class HostLifecycleService
  13. {
  14. private static readonly Lazy<IHostLifecycleNotifications> Notifications = new Lazy<IHostLifecycleNotifications>(InitializeNotifications);
  15. private static int _refCount;
  16. /// <summary>
  17. /// Event that gets raised when the host suspends the application.
  18. /// </summary>
  19. public static event EventHandler<HostSuspendingEventArgs> Suspending;
  20. /// <summary>
  21. /// Event that gets raised when the host resumes the application.
  22. /// </summary>
  23. public static event EventHandler<HostResumingEventArgs> Resuming;
  24. /// <summary>
  25. /// Adds a reference to the host lifecycle manager, causing it to be sending notifications.
  26. /// </summary>
  27. public static void AddRef()
  28. {
  29. if (Interlocked.Increment(ref _refCount) == 1)
  30. {
  31. var notifications = Notifications.Value;
  32. if (notifications != null)
  33. {
  34. notifications.Suspending += OnSuspending;
  35. notifications.Resuming += OnResuming;
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Removes a reference to the host lifecycle manager, causing it to stop sending notifications
  41. /// if the removed reference was the last one.
  42. /// </summary>
  43. public static void Release()
  44. {
  45. if (Interlocked.Decrement(ref _refCount) == 0)
  46. {
  47. var notifications = Notifications.Value;
  48. if (notifications != null)
  49. {
  50. notifications.Suspending -= OnSuspending;
  51. notifications.Resuming -= OnResuming;
  52. }
  53. }
  54. }
  55. private static void OnSuspending(object sender, HostSuspendingEventArgs e)
  56. {
  57. Suspending?.Invoke(sender, e);
  58. }
  59. private static void OnResuming(object sender, HostResumingEventArgs e)
  60. {
  61. Resuming?.Invoke(sender, e);
  62. }
  63. private static IHostLifecycleNotifications InitializeNotifications()
  64. {
  65. #pragma warning disable CS0618 // Type or member is obsolete
  66. return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>();
  67. #pragma warning restore CS0618 // Type or member is obsolete
  68. }
  69. }
  70. /// <summary>
  71. /// (Infrastructure) Provides notifications about the host's lifecycle events.
  72. /// </summary>
  73. [EditorBrowsable(EditorBrowsableState.Never)]
  74. public interface IHostLifecycleNotifications
  75. {
  76. /// <summary>
  77. /// Event that gets raised when the host suspends.
  78. /// </summary>
  79. event EventHandler<HostSuspendingEventArgs> Suspending;
  80. /// <summary>
  81. /// Event that gets raised when the host resumes.
  82. /// </summary>
  83. event EventHandler<HostResumingEventArgs> Resuming;
  84. }
  85. /// <summary>
  86. /// (Infrastructure) Event arguments for host suspension events.
  87. /// </summary>
  88. [EditorBrowsable(EditorBrowsableState.Never)]
  89. public class HostSuspendingEventArgs : EventArgs
  90. {
  91. }
  92. /// <summary>
  93. /// (Infrastructure) Event arguments for host resumption events.
  94. /// </summary>
  95. [EditorBrowsable(EditorBrowsableState.Never)]
  96. public class HostResumingEventArgs : EventArgs
  97. {
  98. }
  99. }