| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.InteropServices.ComTypes; |
| | | 4 | | using System.Threading; |
| | | 5 | | |
| | | 6 | | namespace SolidEdgeCommunity; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Default controller that handles connecting\disconnecting to COM events via IConnectionPointContainer and IConnection |
| | | 10 | | /// </summary> |
| | | 11 | | public class ConnectionPointController |
| | | 12 | | { |
| | | 13 | | private readonly object _sink; |
| | 4 | 14 | | private readonly Dictionary<IConnectionPoint, int> _connectionPointDictionary = []; |
| | | 15 | | |
| | 4 | 16 | | public ConnectionPointController(object sink) |
| | | 17 | | { |
| | 4 | 18 | | ArgumentNullException.ThrowIfNull(sink); |
| | | 19 | | |
| | 4 | 20 | | _sink = sink; |
| | 4 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Establishes a connection between a connection point object and the client's sink. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <typeparam name="TInterface">Interface type of the outgoing interface whose connection point object is being req |
| | | 27 | | /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param> |
| | | 28 | | public void AdviseSink<TInterface>(object container) where TInterface : class |
| | | 29 | | { |
| | 5 | 30 | | bool lockTaken = false; |
| | | 31 | | |
| | | 32 | | try |
| | | 33 | | { |
| | 5 | 34 | | Monitor.Enter(this, ref lockTaken); |
| | | 35 | | |
| | | 36 | | // Prevent multiple event Advise() calls on same sink. |
| | 5 | 37 | | if (IsSinkAdvised<TInterface>(container)) |
| | | 38 | | { |
| | 1 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | IConnectionPointContainer cpc = null; |
| | 4 | 43 | | int cookie = 0; |
| | | 44 | | |
| | 4 | 45 | | cpc = (IConnectionPointContainer)container; |
| | 4 | 46 | | var item = typeof(TInterface).GUID; |
| | 4 | 47 | | cpc.FindConnectionPoint(ref item, out IConnectionPoint cp); |
| | | 48 | | |
| | 4 | 49 | | if (cp != null) |
| | | 50 | | { |
| | 4 | 51 | | cp.Advise(_sink, out cookie); |
| | 4 | 52 | | _connectionPointDictionary.Add(cp, cookie); |
| | | 53 | | } |
| | 4 | 54 | | } |
| | | 55 | | finally |
| | | 56 | | { |
| | 5 | 57 | | if (lockTaken) |
| | | 58 | | { |
| | 5 | 59 | | Monitor.Exit(this); |
| | | 60 | | } |
| | 5 | 61 | | } |
| | 5 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Determines if a connection between a connection point object and the client's sink is established. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param> |
| | | 68 | | public bool IsSinkAdvised<TInterface>(object container) where TInterface : class |
| | | 69 | | { |
| | 9 | 70 | | bool lockTaken = false; |
| | | 71 | | |
| | | 72 | | try |
| | | 73 | | { |
| | 9 | 74 | | Monitor.Enter(this, ref lockTaken); |
| | | 75 | | |
| | | 76 | | IConnectionPointContainer cpc = null; |
| | | 77 | | int cookie = 0; |
| | | 78 | | |
| | 9 | 79 | | cpc = (IConnectionPointContainer)container; |
| | 9 | 80 | | var item = typeof(TInterface).GUID; |
| | 9 | 81 | | cpc.FindConnectionPoint(ref item, out IConnectionPoint cp); |
| | | 82 | | |
| | 9 | 83 | | return (cp != null) && _connectionPointDictionary.ContainsKey(cp); |
| | | 84 | | } |
| | | 85 | | finally |
| | | 86 | | { |
| | 9 | 87 | | if (lockTaken) |
| | | 88 | | { |
| | 9 | 89 | | Monitor.Exit(this); |
| | | 90 | | } |
| | 9 | 91 | | } |
| | 9 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Terminates an advisory connection previously established between a connection point object and a client's sink. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <typeparam name="TInterface">Interface type of the interface whose connection point object is being requested to |
| | | 98 | | /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param> |
| | | 99 | | public void UnadviseSink<TInterface>(object container) where TInterface : class |
| | | 100 | | { |
| | 1 | 101 | | bool lockTaken = false; |
| | | 102 | | |
| | | 103 | | try |
| | | 104 | | { |
| | 1 | 105 | | Monitor.Enter(this, ref lockTaken); |
| | | 106 | | |
| | | 107 | | IConnectionPointContainer cpc = null; |
| | | 108 | | |
| | 1 | 109 | | cpc = (IConnectionPointContainer)container; |
| | 1 | 110 | | var item = typeof(TInterface).GUID; |
| | 1 | 111 | | cpc.FindConnectionPoint(ref item, out IConnectionPoint cp); |
| | | 112 | | |
| | 1 | 113 | | if (cp != null && _connectionPointDictionary.TryGetValue(cp, out int cookie)) |
| | | 114 | | { |
| | 1 | 115 | | cp.Unadvise(cookie); |
| | 1 | 116 | | _connectionPointDictionary.Remove(cp); |
| | | 117 | | } |
| | 1 | 118 | | } |
| | | 119 | | finally |
| | | 120 | | { |
| | 1 | 121 | | if (lockTaken) |
| | | 122 | | { |
| | 1 | 123 | | Monitor.Exit(this); |
| | | 124 | | } |
| | 1 | 125 | | } |
| | 1 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Terminates all advisory connections previously established. |
| | | 130 | | /// </summary> |
| | | 131 | | public void UnadviseAllSinks() |
| | | 132 | | { |
| | 1 | 133 | | bool lockTaken = false; |
| | | 134 | | |
| | | 135 | | try |
| | | 136 | | { |
| | 1 | 137 | | Monitor.Enter(this, ref lockTaken); |
| | 1 | 138 | | Dictionary<IConnectionPoint, int>.Enumerator enumerator = _connectionPointDictionary.GetEnumerator(); |
| | 2 | 139 | | while (enumerator.MoveNext()) |
| | | 140 | | { |
| | 1 | 141 | | enumerator.Current.Key.Unadvise(enumerator.Current.Value); |
| | | 142 | | } |
| | 1 | 143 | | } |
| | | 144 | | finally |
| | | 145 | | { |
| | 1 | 146 | | _connectionPointDictionary.Clear(); |
| | | 147 | | |
| | 1 | 148 | | if (lockTaken) |
| | | 149 | | { |
| | 1 | 150 | | Monitor.Exit(this); |
| | | 151 | | } |
| | 1 | 152 | | } |
| | 1 | 153 | | } |
| | | 154 | | } |