HostLifecycleService
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Reactive.PlatformServices
{
[NullableContext(1)]
[Nullable(0)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HostLifecycleService
{
[Nullable(0)]
private class DelegatingHostLifecycleNotifications : IHostLifecycleNotifications
{
private readonly object _lock = new object();
[Nullable(new byte[] {
2,
1
})]
private EventHandler<HostSuspendingEventArgs> _suspending;
[Nullable(new byte[] {
2,
1
})]
private EventHandler<HostResumingEventArgs> _resuming;
[Nullable(2)]
private IHostLifecycleNotifications _source;
public event EventHandler<HostSuspendingEventArgs> Suspending {
add {
lock (_lock) {
_suspending = value;
if (_source != null)
_source.Suspending += value;
}
}
remove {
lock (_lock) {
if (_source != null)
_source.Suspending -= value;
_suspending = null;
}
}
}
public event EventHandler<HostResumingEventArgs> Resuming {
add {
lock (_lock) {
if (_source != null)
_source.Resuming += value;
_resuming = value;
}
}
remove {
lock (_lock) {
if (_source != null)
_source.Resuming -= value;
_resuming = null;
}
}
}
internal bool TrySetHostLifecycleNotifications(IHostLifecycleNotifications notifications)
{
lock (_lock) {
if (_source == null) {
_source = notifications;
if (_suspending != null)
_source.Suspending += _suspending;
if (_resuming != null)
_source.Resuming += _resuming;
return true;
}
return false;
}
}
}
[Nullable(new byte[] {
1,
2
})]
private static readonly Lazy<IHostLifecycleNotifications> Notifications = new Lazy<IHostLifecycleNotifications>(InitializeNotifications);
private static int _refCount;
[Nullable(new byte[] {
2,
1
})]
[method: Nullable(new byte[] {
2,
1
})]
[field: Nullable(new byte[] {
2,
1
})]
public static event EventHandler<HostSuspendingEventArgs> Suspending;
[Nullable(new byte[] {
2,
1
})]
[method: Nullable(new byte[] {
2,
1
})]
[field: 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;
}
}
}
public static bool TrySetHostLifecycleNotifications(IHostLifecycleNotifications notifications)
{
return (Notifications.Value as DelegatingHostLifecycleNotifications)?.TrySetHostLifecycleNotifications(notifications) ?? false;
}
private static void OnSuspending([Nullable(2)] object sender, HostSuspendingEventArgs e)
{
HostLifecycleService.Suspending?.Invoke(sender, e);
}
private static void OnResuming([Nullable(2)] object sender, HostResumingEventArgs e)
{
HostLifecycleService.Resuming?.Invoke(sender, e);
}
[NullableContext(2)]
private static IHostLifecycleNotifications InitializeNotifications()
{
return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>(Array.Empty<object>()) ?? new DelegatingHostLifecycleNotifications();
}
}
}