HostLifecycleService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>();
  70. }
  71. }
  72. /// <summary>
  73. /// (Infrastructure) Provides notifications about the host's lifecycle events.
  74. /// </summary>
  75. [EditorBrowsable(EditorBrowsableState.Never)]
  76. public interface IHostLifecycleNotifications
  77. {
  78. /// <summary>
  79. /// Event that gets raised when the host suspends.
  80. /// </summary>
  81. event EventHandler<HostSuspendingEventArgs> Suspending;
  82. /// <summary>
  83. /// Event that gets raised when the host resumes.
  84. /// </summary>
  85. event EventHandler<HostResumingEventArgs> Resuming;
  86. }
  87. /// <summary>
  88. /// (Infrastructure) Event arguments for host suspension events.
  89. /// </summary>
  90. [EditorBrowsable(EditorBrowsableState.Never)]
  91. public class HostSuspendingEventArgs : EventArgs
  92. {
  93. }
  94. /// <summary>
  95. /// (Infrastructure) Event arguments for host resumption events.
  96. /// </summary>
  97. [EditorBrowsable(EditorBrowsableState.Never)]
  98. public class HostResumingEventArgs : EventArgs
  99. {
  100. }
  101. }