// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license 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 Lazy s_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 = s_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 = s_notifications.Value;
if (notifications != null)
{
notifications.Suspending -= OnSuspending;
notifications.Resuming -= OnResuming;
}
}
}
private static void OnSuspending(object sender, HostSuspendingEventArgs e)
{
var suspending = Suspending;
if (suspending != null)
suspending(sender, e);
}
private static void OnResuming(object sender, HostResumingEventArgs e)
{
var resuming = Resuming;
if (resuming != null)
resuming(sender, e);
}
private static IHostLifecycleNotifications InitializeNotifications()
{
return PlatformEnlightenmentProvider.Current.GetService();
}
}
///
/// (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
{
}
}