1
1
using System ;
2
2
using System . Collections ;
3
+ using System . Collections . Concurrent ;
3
4
using System . Collections . Generic ;
4
5
using System . IO ;
5
6
using System . Linq ;
@@ -14,6 +15,8 @@ namespace Rubberduck.VBEditor.SafeComWrappers.VBA
14
15
public class VBComponents : SafeComWrapper < VB . VBComponents > , IVBComponents
15
16
{
16
17
private static readonly Guid VBComponentsEventsGuid = new Guid ( "0002E116-0000-0000-C000-000000000046" ) ;
18
+ private static HashSet < VB . VBComponents > _handlers = new HashSet < VB . VBComponents > ( ) ;
19
+ private static object _lockObject = new object ( ) ;
17
20
18
21
private enum ComponentEventDispId
19
22
{
@@ -192,38 +195,43 @@ public void RemoveSafely(IVBComponent component)
192
195
193
196
#region Events
194
197
195
- private static bool _eventsAttached ;
196
198
private static void AttachEvents ( VB . VBComponents components )
197
199
{
198
- if ( ! _eventsAttached )
200
+ lock ( _lockObject )
199
201
{
200
- _componentAdded = OnComponentAdded ;
201
- _componentRemoved = OnComponentRemoved ;
202
- _componentRenamed = OnComponentRenamed ;
203
- _componentSelected = OnComponentSelected ;
204
- _componentActivated = OnComponentActivated ;
205
- _componentReloaded = OnComponentReloaded ;
206
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemAdded , _componentAdded ) ;
207
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRemoved , _componentRemoved ) ;
208
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRenamed , _componentRenamed ) ;
209
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemSelected , _componentSelected ) ;
210
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemActivated , _componentActivated ) ;
211
- ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemReloaded , _componentReloaded ) ;
212
- _eventsAttached = true ;
202
+ if ( ! _handlers . Contains ( components ) )
203
+ {
204
+ _componentAdded = OnComponentAdded ;
205
+ _componentRemoved = OnComponentRemoved ;
206
+ _componentRenamed = OnComponentRenamed ;
207
+ _componentSelected = OnComponentSelected ;
208
+ _componentActivated = OnComponentActivated ;
209
+ _componentReloaded = OnComponentReloaded ;
210
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemAdded , _componentAdded ) ;
211
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRemoved , _componentRemoved ) ;
212
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRenamed , _componentRenamed ) ;
213
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemSelected , _componentSelected ) ;
214
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemActivated , _componentActivated ) ;
215
+ ComEventsHelper . Combine ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemReloaded , _componentReloaded ) ;
216
+ _handlers . Add ( components ) ;
217
+ }
213
218
}
214
219
}
215
220
216
221
private static void DetatchEvents ( VB . VBComponents components )
217
222
{
218
- if ( ! _eventsAttached )
223
+ lock ( _lockObject )
219
224
{
220
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemAdded , _componentAdded ) ;
221
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRemoved , _componentRemoved ) ;
222
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRenamed , _componentRenamed ) ;
223
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemSelected , _componentSelected ) ;
224
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemActivated , _componentActivated ) ;
225
- ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemReloaded , _componentReloaded ) ;
226
- _eventsAttached = false ;
225
+ if ( _handlers . Contains ( components ) )
226
+ {
227
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemAdded , _componentAdded ) ;
228
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRemoved , _componentRemoved ) ;
229
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemRenamed , _componentRenamed ) ;
230
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemSelected , _componentSelected ) ;
231
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemActivated , _componentActivated ) ;
232
+ ComEventsHelper . Remove ( components , VBComponentsEventsGuid , ( int ) ComponentEventDispId . ItemReloaded , _componentReloaded ) ;
233
+ _handlers . Remove ( components ) ;
234
+ }
227
235
}
228
236
}
229
237
0 commit comments