HostLifecycleService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.ComponentModel;
  3. using System.Threading;
  4. namespace System.Reactive.PlatformServices
  5. {
  6. /// <summary>
  7. /// (Infrastructure) Provides access to the host's lifecycle management services.
  8. /// </summary>
  9. [EditorBrowsable(EditorBrowsableState.Never)]
  10. public static class HostLifecycleService
  11. {
  12. private static Lazy<IHostLifecycleNotifications> s_notifications = new Lazy<IHostLifecycleNotifications>(InitializeNotifications);
  13. private static int _refCount;
  14. /// <summary>
  15. /// Event that gets raised when the host suspends the application.
  16. /// </summary>
  17. public static event EventHandler<HostSuspendingEventArgs> Suspending;
  18. /// <summary>
  19. /// Event that gets raised when the host resumes the application.
  20. /// </summary>
  21. public static event EventHandler<HostResumingEventArgs> Resuming;
  22. /// <summary>
  23. /// Adds a reference to the host lifecycle manager, causing it to be sending notifications.
  24. /// </summary>
  25. public static void AddRef()
  26. {
  27. if (Interlocked.Increment(ref _refCount) == 1)
  28. {
  29. var notifications = s_notifications.Value;
  30. if (notifications != null)
  31. {
  32. notifications.Suspending += OnSuspending;
  33. notifications.Resuming += OnResuming;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Removes a reference to the host lifecycle manager, causing it to stop sending notifications
  39. /// if the removed reference was the last one.
  40. /// </summary>
  41. public static void Release()
  42. {
  43. if (Interlocked.Decrement(ref _refCount) == 0)
  44. {
  45. var notifications = s_notifications.Value;
  46. if (notifications != null)
  47. {
  48. notifications.Suspending -= OnSuspending;
  49. notifications.Resuming -= OnResuming;
  50. }
  51. }
  52. }
  53. private static void OnSuspending(object sender, HostSuspendingEventArgs e)
  54. {
  55. var suspending = Suspending;
  56. if (suspending != null)
  57. suspending(sender, e);
  58. }
  59. private static void OnResuming(object sender, HostResumingEventArgs e)
  60. {
  61. var resuming = Resuming;
  62. if (resuming != null)
  63. resuming(sender, e);
  64. }
  65. private static IHostLifecycleNotifications InitializeNotifications()
  66. {
  67. return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>();
  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. }