Handle When COM Object Is Disconnected From RCW?Feb 15, 2012
Is there any way to run code when COM object is about to be disconnected from RCW without implementing IDisposale interface and explicit call MyObject.Dispose in client code?
The code below is not working. When program enters Finalize sub, ExcelApplication is already disconnected from RCW, and I get error. I understand why it happens, but I want to get a solution.
I want to make it working the following way: when last managed reference is released, code from Finalize sub is executed before COM object is disconnected.
My question is - is it possible at all and if it is then how can I handle this event inside the class, without need to call Finalize explicitly from outside?
Very simple class:
Public Class ExcelRunner
Dim ExcelApplication As Excel.Application
Public Sub New()