HostLifecycleService
(Infrastructure) Provides access to the host's lifecycle management services.
            
                using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Reactive.PlatformServices
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public static class HostLifecycleService
    {
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            1,
            2
        })]
        private static readonly Lazy<IHostLifecycleNotifications> Notifications = new Lazy<IHostLifecycleNotifications>(InitializeNotifications);
        private static int _refCount;
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        [method: System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        [field: System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        public static event EventHandler<HostSuspendingEventArgs> Suspending;
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        [method: System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        [field: System.Runtime.CompilerServices.Nullable(new byte[] {
            2,
            1
        })]
        public static event EventHandler<HostResumingEventArgs> Resuming;
        public static void AddRef()
        {
            if (Interlocked.Increment(ref _refCount) == 1) {
                IHostLifecycleNotifications value = Notifications.Value;
                if (value != null) {
                    value.Suspending += OnSuspending;
                    value.Resuming += OnResuming;
                }
            }
        }
        public static void Release()
        {
            if (Interlocked.Decrement(ref _refCount) == 0) {
                IHostLifecycleNotifications value = Notifications.Value;
                if (value != null) {
                    value.Suspending -= OnSuspending;
                    value.Resuming -= OnResuming;
                }
            }
        }
        private static void OnSuspending([System.Runtime.CompilerServices.Nullable(2)] object sender, HostSuspendingEventArgs e)
        {
            HostLifecycleService.Suspending?.Invoke(sender, e);
        }
        private static void OnResuming([System.Runtime.CompilerServices.Nullable(2)] object sender, HostResumingEventArgs e)
        {
            HostLifecycleService.Resuming?.Invoke(sender, e);
        }
        [System.Runtime.CompilerServices.NullableContext(2)]
        private static IHostLifecycleNotifications InitializeNotifications()
        {
            return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>(Array.Empty<object>());
        }
    }
}