1: public class NetworkWatchdog : IApplicationService
2: {
3: private object _lockObj = new object();
4: public event EventHandler<NetworkChangedEventArgs> NetworkChanged;
5:
6: /// <summary>
7: /// Gets the current.
8: /// </summary>
9: public static NetworkWatchdog Current { get; private set; }
10: /// <summary>
11: /// Gets a value indicating whether this instance is network available.
12: /// </summary>
13: /// <value>
14: /// <c>true</c> if this instance is network available; otherwise, <c>false</c>.
15: /// </value>
16: public bool IsNetworkAvailable { get; private set; }
17: /// <summary>
18: /// Gets the type of the interface.
19: /// </summary>
20: /// <value>
21: /// The type of the interface.
22: /// </value>
23: public NetworkInterfaceType InterfaceType { get; private set; }
24:
25: /// <summary>
26: /// Initializes a new instance of the <see cref="NetworkWatchdog"/> class.
27: /// </summary>
28: public NetworkWatchdog()
29: {
30: if (NetworkWatchdog.Current != null)
31: throw new Exception("Only a single watchdog is allowed");
32:
33: NetworkWatchdog.Current = this;
34: }
35:
36: /// <summary>
37: /// Called by an application in order to initialize the application extension service.
38: /// </summary>
39: /// <param name="context">Provides information about the application state.</param>
40: public void StartService(ApplicationServiceContext context)
41: {
42: System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged +=
43: new System.Net.NetworkInformation.NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
44:
45: ThreadPool.QueueUserWorkItem(ReadNetworkInformation, null);
46: }
47:
48: /// <summary>
49: /// Called by an application in order to stop the application extension service.
50: /// </summary>
51: public void StopService()
52: {
53: System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged -=
54: new System.Net.NetworkInformation.NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
55: }
56:
57: /// <summary>
58: /// Handles the NetworkAddressChanged event of the NetworkChange control.
59: /// </summary>
60: /// <param name="sender">The source of the event.</param>
61: /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
62: private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
63: {
64: ThreadPool.QueueUserWorkItem(ReadNetworkInformation, null);
65: }
66:
67: /// <summary>
68: /// Reads the network information.
69: /// </summary>
70: /// <param name="state">The state.</param>
71: private void ReadNetworkInformation(object state)
72: {
73: try
74: {
75: bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
76: NetworkInterfaceType interfaceType = NetworkInterface.NetworkInterfaceType;
77:
78: bool isAvailableChanged = false;
79: bool isTypeChanged = false;
80:
81: lock (_lockObj)
82: {
83: if (this.IsNetworkAvailable != isAvailable)
84: {
85: this.IsNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
86: isAvailableChanged = true;
87: }
88:
89: if (this.InterfaceType != interfaceType)
90: {
91: this.InterfaceType = interfaceType;
92: isTypeChanged = true;
93: }
94: }
95:
96: if (isAvailableChanged || isTypeChanged)
97: this.OnNetworkChanged(isAvailableChanged, isTypeChanged);
98: }
99: catch (Exception ex)
100: {
101: Deployment.Current.Dispatcher.BeginInvoke(
102: () => MessageBox.Show(ex.ToString()));
103: }
104: }
105:
106: /// <summary>
107: /// Called when [network changed].
108: /// </summary>
109: /// <param name="isAvailableChanged">if set to <c>true</c> [is available changed].</param>
110: /// <param name="isTypeChanged">if set to <c>true</c> [is type changed].</param>
111: private void OnNetworkChanged(bool isAvailableChanged, bool isTypeChanged)
112: {
113: EventHandler<NetworkChangedEventArgs> handler = this.NetworkChanged;
114:
115: if (handler != null)
116: Deployment.Current.Dispatcher.BeginInvoke(
117: () => handler(this, new NetworkChangedEventArgs(isAvailableChanged, isTypeChanged)));
118: }
119: }
120:
121: public class NetworkChangedEventArgs : EventArgs
122: {
123: /// <summary>
124: /// Gets or sets a value indicating whether this instance is availability changed.
125: /// </summary>
126: /// <value>
127: /// <c>true</c> if this instance is availability changed; otherwise, <c>false</c>.
128: /// </value>
129: public bool IsAvailabilityChanged { get; set; }
130: /// <summary>
131: /// Gets or sets a value indicating whether this instance is network type changed.
132: /// </summary>
133: /// <value>
134: /// <c>true</c> if this instance is network type changed; otherwise, <c>false</c>.
135: /// </value>
136: public bool IsNetworkTypeChanged { get; set; }
137:
138: /// <summary>
139: /// Initializes a new instance of the <see cref="NetworkChangedEventArgs"/> class.
140: /// </summary>
141: /// <param name="isAvailabilityChanged">if set to <c>true</c> [is availability changed].</param>
142: /// <param name="isNetworkTypeChanged">if set to <c>true</c> [is network type changed].</param>
143: public NetworkChangedEventArgs(bool isAvailabilityChanged, bool isNetworkTypeChanged)
144: {
145: this.IsAvailabilityChanged = isAvailabilityChanged;
146: this.IsNetworkTypeChanged = isNetworkTypeChanged;
147: }
148: }