HostLifecycleService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 Lazy<IHostLifecycleNotifications> s_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 = s_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 = s_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. var suspending = Suspending;
  58. if (suspending != null)
  59. suspending(sender, e);
  60. }
  61. private static void OnResuming(object sender, HostResumingEventArgs e)
  62. {
  63. var resuming = Resuming;
  64. if (resuming != null)
  65. resuming(sender, e);
  66. }
  67. private static IHostLifecycleNotifications InitializeNotifications()
  68. {
  69. #pragma warning disable CS0618 // Type or member is obsolete
  70. return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>();
  71. #pragma warning restore CS0618 // Type or member is obsolete
  72. }
  73. }
  74. /// <summary>
  75. /// (Infrastructure) Provides notifications about the host's lifecycle events.
  76. /// </summary>
  77. [EditorBrowsable(EditorBrowsableState.Never)]
  78. public interface IHostLifecycleNotifications
  79. {
  80. /// <summary>
  81. /// Event that gets raised when the host suspends.
  82. /// </summary>
  83. event EventHandler<HostSuspendingEventArgs> Suspending;
  84. /// <summary>
  85. /// Event that gets raised when the host resumes.
  86. /// </summary>
  87. event EventHandler<HostResumingEventArgs> Resuming;
  88. }
  89. /// <summary>
  90. /// (Infrastructure) Event arguments for host suspension events.
  91. /// </summary>
  92. [EditorBrowsable(EditorBrowsableState.Never)]
  93. public class HostSuspendingEventArgs : EventArgs
  94. {
  95. }
  96. /// <summary>
  97. /// (Infrastructure) Event arguments for host resumption events.
  98. /// </summary>
  99. [EditorBrowsable(EditorBrowsableState.Never)]
  100. public class HostResumingEventArgs : EventArgs
  101. {
  102. }
  103. }