// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Threading;
namespace System.Reactive.PlatformServices
{
///
/// (Infrastructure) Provides access to the host's lifecycle management services.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HostLifecycleService
{
private static readonly Lazy Notifications = new Lazy(InitializeNotifications);
private static int _refCount;
///
/// Event that gets raised when the host suspends the application.
///
public static event EventHandler Suspending;
///
/// Event that gets raised when the host resumes the application.
///
public static event EventHandler Resuming;
///
/// Adds a reference to the host lifecycle manager, causing it to be sending notifications.
///
public static void AddRef()
{
if (Interlocked.Increment(ref _refCount) == 1)
{
var notifications = Notifications.Value;
if (notifications != null)
{
notifications.Suspending += OnSuspending;
notifications.Resuming += OnResuming;
}
}
}
///
/// Removes a reference to the host lifecycle manager, causing it to stop sending notifications
/// if the removed reference was the last one.
///
public static void Release()
{
if (Interlocked.Decrement(ref _refCount) == 0)
{
var notifications = Notifications.Value;
if (notifications != null)
{
notifications.Suspending -= OnSuspending;
notifications.Resuming -= OnResuming;
}
}
}
private static void OnSuspending(object sender, HostSuspendingEventArgs e)
{
Suspending?.Invoke(sender, e);
}
private static void OnResuming(object sender, HostResumingEventArgs e)
{
Resuming?.Invoke(sender, e);
}
private static IHostLifecycleNotifications InitializeNotifications()
{
#pragma warning disable CS0618 // Type or member is obsolete
return PlatformEnlightenmentProvider.Current.GetService();
#pragma warning restore CS0618 // Type or member is obsolete
}
}
///
/// (Infrastructure) Provides notifications about the host's lifecycle events.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IHostLifecycleNotifications
{
///
/// Event that gets raised when the host suspends.
///
event EventHandler Suspending;
///
/// Event that gets raised when the host resumes.
///
event EventHandler Resuming;
}
///
/// (Infrastructure) Event arguments for host suspension events.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public class HostSuspendingEventArgs : EventArgs
{
}
///
/// (Infrastructure) Event arguments for host resumption events.
///
[EditorBrowsable(EditorBrowsableState.Never)]
public class HostResumingEventArgs : EventArgs
{
}
}