Events in VSTA Part IV: Unloading Add-ins that do not Unhook from Events

As mentioned in Part III, any events an add-in hooks into should be unhooked in the add-in, presumably in the Shutdown method.  Unfortunately, if the add-in does not unhook from an event this can cause problems- namely that the host application will continue to attempt to reach the add-in code when the event is raised.

To prevent these problems, the host application should do two things:   1) Shutdown the add-in’s controller and 2) Use an event helper.  This post will cover shutting down controllers when unloading add-ins, event helpers will be covered in the next post (when an add-in hooked into an event is unloaded with its controller shutdown, raising the event will result in a “The target application domain has been unloaded” error- the EventHelper solves this).

C#:
internal void ShutdownAddInsCntlrs()
{
   //for each add-in
  
foreach (IEntryPoint ep in this.addInList)
   {
      //shutdown the add-in
     
ep.OnShutdown(); 

      //get the contorller and shut it down
     
AddInController aic = AddInController.GetAddInController(ep);
      aic.Shutdown();
   }

    //clear the list of add-ins
  
this.addInList.Clear();
}

//list of add-ins
private
List<IEntryPoint> addInList


VB:
Friend Sub ShutdownAddInsCntlrs()

    'for each add-in
   
For Each ep As IEntryPoint In Me.addInList

        'shutdown the add-in
       
ep.OnShutdown() 

        'get the contorller and shut it down
       
AddInController(aic = AddInController.GetAddInController(ep))
        aic.Shutdown()

    Next 

    'clear the list of add-ins
   
this.addInList.Clear()

End Sub

'list of add-ins
Private
addInList As List(Of IEntryPoint)

 

ShapeApp Samples:
The ShapeApp samples included with the SDK do not use the above method to unload “regular” add-ins (the above method is used for macro and DPM add-ins).  The ShapeApp host application only unloads “regular” add-ins when the host is terminating; therefore, there is no opportunity for an unloaded add-in still hooked into an event to cause problems because no exposed events are fired after the add-ins are unloaded.  If a host uses this method, does unload add-ins and then fires an event that an unloaded add-in is hooked into, the add-in code hooked into the event will be called and run.  Below is the method used by the ShapeApp samples- only use this method only if no exposed events will be fired after the add-ins are unloaded.

C#:
internal void ShutdownAddIns()
{
   //Shutdown each add-in
   foreach (IEntryPoint ep in this.addInList)
   {
      ep.OnShutdown();
   } 

   //Clear the list of add-ins
   this.addInList.Clear();
}

//list of add-ins
private
List<IEntryPoint> addInList;

 
VB:
Friend Sub ShutdownAddIns()

    'Shutdown each add-in
   
For Each ep As IEntryPoint In Me.addInList
        ep.OnShutdown()
    Next

    'Clear the add-in list
   
Me.addInList.Clear()

End Sub

'list of add-ins
Private
addInList As List(Of IEntryPoint)


Posted May 16 2008, 01:15 PM by Melody
Copyright Summit Software Company, 2008. All rights reserved.