07/17/2012

Cannot remove .NET event handler

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

PublicClassCommands

ImplementsIExtensionApplication

PublicSub Initialize() ImplementsIExtensionApplication.Initialize

AddHandler _

Application.DocumentManager.DocumentToBeDestroyed, _

AddressOf docBeginDocClose

EndSub

PublicSub Terminate() ImplementsIExtensionApplication.Terminate

EndSub

PublicSub docBeginDocClose( _

ByVal senderObj AsObject, _

ByVal docColDocActEvtArgs AsDocumentCollectionEventArgs)

System.Diagnostics.Debug.Print("in docBeginDocClose")

EndSub

<CommandMethod("StopEvent")> _

PublicSub StopEvents()

Try

RemoveHandler _

Application.DocumentManager.DocumentToBeDestroyed, _

AddressOf docBeginDocClose

Catch ex AsException

MsgBox("Error: " & ex.Message)

EndTry

EndSub

EndClass

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):

Comments

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

PublicClassCommands

ImplementsIExtensionApplication

PublicSub Initialize() ImplementsIExtensionApplication.Initialize

AddHandler _

Application.DocumentManager.DocumentToBeDestroyed, _

AddressOf docBeginDocClose

EndSub

PublicSub Terminate() ImplementsIExtensionApplication.Terminate

EndSub

PublicSub docBeginDocClose( _

ByVal senderObj AsObject, _

ByVal docColDocActEvtArgs AsDocumentCollectionEventArgs)

System.Diagnostics.Debug.Print("in docBeginDocClose")

EndSub

<CommandMethod("StopEvent")> _

PublicSub StopEvents()

Try

RemoveHandler _

Application.DocumentManager.DocumentToBeDestroyed, _

AddressOf docBeginDocClose

Catch ex AsException

MsgBox("Error: " & ex.Message)

EndTry

EndSub

EndClass

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):