I am trying to build a fairly simple ( I think ) add-in wherein I need to
handle the SolutionEvents Opened, AfterClosing, ProjectAdded and
ProjectRemoved events. I have some code that looks like this
public override void OnConnection( object Application , ext_ConnectMode
ConnectMode , object AddInInst , ref Array custom )
{
//call to base captures Application into VSApplication field
base.OnConnection( Application , ConnectMode , AddInInst , ref custom );
VSApplication.Events.SolutionEvents.Opened += new
_dispSolutionEvents_OpenedEventHandler( OnSolutionOpen );
VSApplication.Events.SolutionEvents.AfterClosing += new
_dispSolutionEvents_AfterClosingEventHandler( OnSolutionClosed );
VSApplication.Events.SolutionEvents.ProjectAdded += new
_dispSolutionEvents_ProjectAddedEventHandler( OnProjectAdded );
VSApplication.Events.SolutionEvents.ProjectRemoved += new
_dispSolutionEvents_ProjectRemovedEventHandler( OnProjectRemoved );
}
Problem is that the code above runs fine when the test IDE starts up (add-in
loads on start up). But when I load a solution into it, the OnSolutionOpen
event handler is never invoked.
More interestingly, if in the test IDE I go to Tools > Add-in Manager and
unload and reload the add-in, I get the following error on the line that
tries to attach to the Opened event:
COM object that has been separated from its underlying RCW cannot be used.
at System.Runtime.InteropServices.UCOMIConnectionPoint.Advise(Object
pUnkSink, Int32& pdwCookie)
at
EnvDTE._dispSolutionEvents_EventProvider.add_Opened(_dispSolutionEvents_OpenedEventHandler
A_1)
at
EnvDTE.SolutionEventsClass.add_Opened(_dispSolutionEvents_OpenedEventHandler
A_1)
at MyAddin.OnConnection(Object Application, ext_ConnectMode ConnectMode,
Object AddInInst, Array& custom)
Does anyone have any ideas what might be going on here?
Kenneth Baltrinic - 06 Apr 2007 12:35 GMT
I found my problem. Though I did not find it documented any where, I
noticed that all the examples always captured the SolutionEvents object into
a class variable (field). The examples keep that reference until it is time
to unhook the events in OnDisconnect and used it there to unhook the events
as well. Now that I am doing this instead of calling
Application.Events.SolutionEvents every time, all is working correctly.
>I am trying to build a fairly simple ( I think ) add-in wherein I need to
>handle the SolutionEvents Opened, AfterClosing, ProjectAdded and
[quoted text clipped - 38 lines]
>
> Does anyone have any ideas what might be going on here?
Luis Bascones - 11 Apr 2007 13:51 GMT
The object is not kept alive only to un hook the events, but to allow it to
manage the event handling. This obect is not static or global, and as such
it will keep a list of the event listeners as long as it's alive (someone
holds a reference to it). If one doesn't hold on to it, the GC will
eventually get to it and destroy it.
Carlos Quintero's mztools.com web site has a good pack of info for VS
developers, and this is one of the topics discussed in there. There a lot of
good stuff in there. I can't recommmend it enough.
-LuisB
>I found my problem. Though I did not find it documented any where, I
>noticed that all the examples always captured the SolutionEvents object
[quoted text clipped - 46 lines]
>>
>> Does anyone have any ideas what might be going on here?