Cannot remove .NET event handler

By Adam Nagy

When my AddIn is loaded it starts listening to the DocumentToBeDestroyed event and it works fine.

However, when later on I try to stop listening to this event I do not succeed and so my handler keeps getting called.

Here is my code:

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
 
Public Class Commands
  Implements IExtensionApplication
 
  Public Sub Initialize() Implements IExtensionApplication.Initialize
    AddHandler _
      Application.DocumentManager.DocumentToBeDestroyed, _
      AddressOf docBeginDocClose
  End Sub
 
  Public Sub Terminate() Implements IExtensionApplication.Terminate
  End Sub
 
  Public Sub docBeginDocClose( _
    ByVal senderObj As Object, _
    ByVal docColDocActEvtArgs As DocumentCollectionEventArgs)
 
    System.Diagnostics.Debug.Print("in docBeginDocClose")
  End Sub
 
   _
  Public Sub StopEvents()
    Try
      RemoveHandler _
        Application.DocumentManager.DocumentToBeDestroyed, _
        AddressOf docBeginDocClose
    Catch ex As Exception
      MsgBox("Error: " & ex.Message)
    End Try
  End Sub
 
End Class

Solution

AutoCAD creates an instance of the class that contains the Initialize() function. If the command function is an instance function (not static/shared), then AutoCAD also creates a new instance of the class that contains that function for each document. If the handler is an instance function as well, then you'll get the instance of it that belongs to the same class instance as the command handler.

So you attach one instance’s function to the event inside Initialize() or a command, then you try to remove another instance’s function later on – so you do not remove the function that you attached to the event in the first place.

That’s why your handler keeps being called even after you tried to remove it.

Based on the above, the solution is to simply make the function static (C#) / Shared (VB.NET):

Public Shared Sub docBeginDocClose( _
  ByVal senderObj As Object, _
  ByVal docColDocActEvtArgs As DocumentCollectionEventArgs)
 
  System.Diagnostics.Debug.Print("in docBeginDocClose")
End Sub

Comments

One response to “Cannot remove .NET event handler”

  1. 解决了我的问题,谢谢!

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading