Events in VSTA Part III: Hooking and Unhooking to Events in Add-ins

Events exposed through VSTA can be easily hooked into by add-ins.  Like Visual Studio, VSTA automates hooking into events in both C# and Visual Basic.  Unfortunately unhooking the events is not automated, but should be done explicitly prior to the add-in unloading.   This may be somewhat confusing to the VB oriented end user who may have hooked into the event unknowingly by using the drop downs.  The most logical place to unhook events is in the Shutdown method, which is executed when the add-in is unloaded. 

The examples below show how to properly hook into and unhook from the NewDocumentCreated event from the ShapeAppCSharp SDK samples (VSTA v 2). 

Example C#
//Startup method called when add-in is loaded
private
void AppAddIn_Startup(object sender, EventArgs e)
{
   //Hook into the event
  
this.NewDocumentCreated += new EventHandler(AppAddIn_NewDocumentCreated);
} 

//Automatically generated method from += hookup
void
AppAddIn_NewDocumentCreated(object sender, EventArgs e)
{
   throw new NotImplementedException();
}

//Shutdown called when add-in is unloaded
private
void AppAddIn_Shutdown(object sender, EventArgs e)
{
   //Unhook the event
  
this.NewDocumentCreated -= new EventHandler(AppAddIn_NewDocumentCreated);
}
 

Example VB Using Drop Downs (Handles):
'Automatically generated method created using the drop downs
Private
Sub AppAddIn_NewDocumentCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.NewDocumentCreated
  
'Do something
End
Sub 

'Shutdown method called when add-in is unloaded
Private
Sub AppAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown 

   'Unhook the event from the Handles hookup
  
RemoveHandler Me.NewDocumentCreated, AddressOf AppAddIn_NewDocumentCreated 

End Sub


Example VB  Manual (AddHandler):

'Starup mehtod called when add-in is loaded

Private
Sub AppAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup 

   'hook into the event with AddHandler
  
AddHandler Me.NewDocumentCreated, AddressOf NewDocumentCreatedEvent

End Sub

'Manually generated method for AddHandler hookup
Private
Sub NewDocumentCreatedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
   'Do something

End
Sub 

'Shutdown method called when add-in is unloaded
Private
Sub AppAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

   'Unhook the event from the AddHandler hookup
  
RemoveHandler Me.NewDocumentCreated, AddressOf NewDocumentCreatedEvent

End Sub

 
For add-ins which are only unloaded immediately prior to the host terminating, unhooking the events is not a big issue (as seen in the SDK samples).  It is add-ins which are unloaded during runtime that can be problematic with this- if an event was hooked into and not unhooked prior to unloading, the host will attempt to reach the add-in code when the event is fired.  The host can use an EventHelper to be sure that unhooked events from add-ins do not cause problems.


Posted May 08 2008, 03:05 PM by Melody
Filed under: ,
Copyright Summit Software Company, 2008. All rights reserved.