For production is much better to use it in addin, but for this forum I need some way, how to test the code without any addin implementation.
This example is iLogic script and you can start them in standard way by "Run Rule".
After that the shared variable "DoubleClickHandler" is ensured and its value is instance of class "SketchedSymbolDoubleClickHandler". This is because I need to keep the handler alive between rule edits. (Not necessary when you use it in addin.)
After that I switch the state of handler Active/Inactive.
I implement interface IDisposable for cleanup reason, because I need to remove the event handler. For cleanup I use my own external rule instead of standard SharedVariable.RemoveAll() method. In standard way the list of shared variables is cleared, but if you have an event handler active, the object can not be removed from memory and the event handler is still active.
This is my rule to cleanup SharedVariables object. It is little bit hackish, but it works for debugging purposes.
Dim bindingFlags As Reflection.BindingFlags = System.Reflection.BindingFlags.NonPublic + System.Reflection.BindingFlags.GetField + System.Reflection.BindingFlags.Instance
Dim variables = SharedVariable.GetType().InvokeMember("m_ruleTempStorage", bindingFlags, Nothing, SharedVariable, Nothing)
Logger.Debug(variables.Count)
For Each keyValue In variables
Dim v = keyValue.Value
Logger.Debug(v.GetType().Name)
If TypeOf v Is IDisposable Then
v.Dispose
Logger.Debug(" - Disposed")
End If
Next
SharedVariable.RemoveAll()
The similar solution I use for AddIns also, because I can keep many handlers in one collection and I can clean up them in single For Each loop.