public void Disconnect() { if ((this.connectionPoint != null) && (this.cookie != 0)) { try { this.connectionPoint.Unadvise(this.cookie); } catch (Exception exception) { if (System.Windows.Forms.ClientUtils.IsCriticalException(exception)) { throw; } } finally { this.cookie = 0; } try { Marshal.ReleaseComObject(this.connectionPoint); } catch (Exception exception2) { if (System.Windows.Forms.ClientUtils.IsCriticalException(exception2)) { throw; } } finally { this.connectionPoint = null; } } }
internal ConnectionPointCookie(object source, object sink, System.Type eventInterface, bool throwException) { if (source is System.Windows.Forms.UnsafeNativeMethods.IConnectionPointContainer) { System.Windows.Forms.UnsafeNativeMethods.IConnectionPointContainer container = (System.Windows.Forms.UnsafeNativeMethods.IConnectionPointContainer) source; try { Guid gUID = eventInterface.GUID; if (container.FindConnectionPoint(ref gUID, out this.connectionPoint) != 0) { this.connectionPoint = null; } } catch { this.connectionPoint = null; } if (this.connectionPoint != null) { if ((sink != null) && eventInterface.IsInstanceOfType(sink)) { int num = this.connectionPoint.Advise(sink, ref this.cookie); if (num != 0) { this.cookie = 0; Marshal.ReleaseComObject(this.connectionPoint); this.connectionPoint = null; if (throwException) { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, System.Windows.Forms.SR.GetString("AXNoSinkAdvise", new object[] { eventInterface.Name }), new object[] { num })); } } else { this.threadId = Thread.CurrentThread.ManagedThreadId; } } else if (throwException) { throw new InvalidCastException(System.Windows.Forms.SR.GetString("AXNoSinkImplementation", new object[] { eventInterface.Name })); } } else if (throwException) { throw new ArgumentException(System.Windows.Forms.SR.GetString("AXNoEventInterface", new object[] { eventInterface.Name })); } } else if (throwException) { throw new InvalidCastException(System.Windows.Forms.SR.GetString("AXNoConnectionPointContainer")); } if ((this.connectionPoint == null) || (this.cookie == 0)) { if (this.connectionPoint != null) { Marshal.ReleaseComObject(this.connectionPoint); } if (throwException) { throw new ArgumentException(System.Windows.Forms.SR.GetString("AXNoConnectionPoint", new object[] { eventInterface.Name })); } } }