/// <summary> /// Starts the watcher raising events. /// </summary> private void StartRaisingEvents() { // Retrieve the current state of the connections on the machine. This is used for comparisons to determine what has changed // once the events are raised. this._lastState = RasConnection.GetActiveConnections(); try { this.Register(NativeMethods.RASCN.Connection, NativeMethods.INVALID_HANDLE_VALUE); if (this.Handle == null || this.Handle.IsInvalid) { this.Register(NativeMethods.RASCN.Disconnection, NativeMethods.INVALID_HANDLE_VALUE); } else { this.Register(NativeMethods.RASCN.Disconnection, this.Handle); this.Register(NativeMethods.RASCN.BandwidthAdded, this.Handle); this.Register(NativeMethods.RASCN.BandwidthRemoved, this.Handle); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } }
private void ConnectionStateChanged(object obj, bool timedOut) { lock (this.lockObject) { if (this.EnableRaisingEvents) { try { // Retrieve the active connections to compare against the last state that was checked. ReadOnlyCollection <RasConnection> connections = RasConnection.GetActiveConnections(); RasConnection connection = null; switch (((RasConnectionWatcherStateObject)obj).ChangeType) { case NativeMethods.RASCN.Connection: connection = FindEntry(connections, this._lastState); if (connection != null) { this.OnConnected(new RasConnectionEventArgs(connection)); } this._lastState = connections; break; case NativeMethods.RASCN.Disconnection: // The handle has not been set or it's invalid, the item that has disconnected will need to be // determined. connection = FindEntry(this._lastState, connections); if (connection != null) { this.OnDisconnected(new RasConnectionEventArgs(connection)); } if (this.Handle != null) { // The handle that was being monitored has been disconnected. this.Handle = null; } this._lastState = connections; break; case NativeMethods.RASCN.BandwidthAdded: if (this.Handle != null && !this.Handle.IsInvalid) { this.OnBandwidthAdded(EventArgs.Empty); } break; case NativeMethods.RASCN.BandwidthRemoved: if (this.Handle != null && !this.Handle.IsInvalid) { this.OnBandwidthRemoved(EventArgs.Empty); } break; } } catch (Exception ex) { this.OnError(new System.IO.ErrorEventArgs(ex)); } } } }