| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.ComponentModel; |
| 4 |
using System.Threading; |
| 5 |
|
| 6 |
namespace System.Reactive.PlatformServices |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// (Infrastructure) Provides access to the host's lifecycle management services. |
| 10 |
/// </summary> |
| 11 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 12 |
public static class HostLifecycleService |
| 13 |
{ |
| 14 |
private static Lazy<IHostLifecycleNotifications> s_notifications = new Lazy<IHostLifecycleNotifications>(InitializeNotifications); |
| 15 |
|
| 16 |
private static int _refCount; |
| 17 |
|
| 18 |
/// <summary> |
| 19 |
/// Event that gets raised when the host suspends the application. |
| 20 |
/// </summary> |
| 21 |
public static event EventHandler<HostSuspendingEventArgs> Suspending; |
| 22 |
|
| 23 |
/// <summary> |
| 24 |
/// Event that gets raised when the host resumes the application. |
| 25 |
/// </summary> |
| 26 |
public static event EventHandler<HostResumingEventArgs> Resuming; |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Adds a reference to the host lifecycle manager, causing it to be sending notifications. |
| 30 |
/// </summary> |
| 31 |
public static void AddRef() |
| 32 |
{ |
| 33 |
if (Interlocked.Increment(ref _refCount) == 1) |
| 34 |
{ |
| 35 |
var notifications = s_notifications.Value; |
| 36 |
if (notifications != null) |
| 37 |
{ |
| 38 |
notifications.Suspending += OnSuspending; |
| 39 |
notifications.Resuming += OnResuming; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/// <summary> |
| 45 |
/// Removes a reference to the host lifecycle manager, causing it to stop sending notifications |
| 46 |
/// if the removed reference was the last one. |
| 47 |
/// </summary> |
| 48 |
public static void Release() |
| 49 |
{ |
| 50 |
if (Interlocked.Decrement(ref _refCount) == 0) |
| 51 |
{ |
| 52 |
var notifications = s_notifications.Value; |
| 53 |
if (notifications != null) |
| 54 |
{ |
| 55 |
notifications.Suspending -= OnSuspending; |
| 56 |
notifications.Resuming -= OnResuming; |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private static void OnSuspending(object sender, HostSuspendingEventArgs e) |
| 62 |
{ |
| 63 |
var suspending = Suspending; |
| 64 |
if (suspending != null) |
| 65 |
suspending(sender, e); |
| 66 |
} |
| 67 |
|
| 68 |
private static void OnResuming(object sender, HostResumingEventArgs e) |
| 69 |
{ |
| 70 |
var resuming = Resuming; |
| 71 |
if (resuming != null) |
| 72 |
resuming(sender, e); |
| 73 |
} |
| 74 |
|
| 75 |
private static IHostLifecycleNotifications InitializeNotifications() |
| 76 |
{ |
| 77 |
return PlatformEnlightenmentProvider.Current.GetService<IHostLifecycleNotifications>(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/// <summary> |
| 82 |
/// (Infrastructure) Provides notifications about the host's lifecycle events. |
| 83 |
/// </summary> |
| 84 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 85 |
public interface IHostLifecycleNotifications |
| 86 |
{ |
| 87 |
/// <summary> |
| 88 |
/// Event that gets raised when the host suspends. |
| 89 |
/// </summary> |
| 90 |
event EventHandler<HostSuspendingEventArgs> Suspending; |
| 91 |
|
| 92 |
/// <summary> |
| 93 |
/// Event that gets raised when the host resumes. |
| 94 |
/// </summary> |
| 95 |
event EventHandler<HostResumingEventArgs> Resuming; |
| 96 |
} |
| 97 |
|
| 98 |
/// <summary> |
| 99 |
/// (Infrastructure) Event arguments for host suspension events. |
| 100 |
/// </summary> |
| 101 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 102 |
public class HostSuspendingEventArgs : EventArgs |
| 103 |
{ |
| 104 |
} |
| 105 |
|
| 106 |
/// <summary> |
| 107 |
/// (Infrastructure) Event arguments for host resumption events. |
| 108 |
/// </summary> |
| 109 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 110 |
public class HostResumingEventArgs : EventArgs |
| 111 |
{ |
| 112 |
} |
| 113 |
} |