I have a com.interop class that inherits objectbase which increments a count. The destructor in objectbase Decrements the count. In order for the destructor to run I have to call GC.Collect on a timer every 5 seconds to let it release it. When the count reaches 0 it means I can close the UI app so it's not on the screen. The maximum count at one time reaches from 2-10 and can last for hours. Running GC.Collect every 5 seconds seems like overkill and a small CPU hit but I can't figure out another way to know the reference is gone. Is there another way that might work?
Asked
Active
Viewed 43 times
0
-
Why don't you implement [IDisposable](https://stackoverflow.com/a/538238/4430204) pattern? You can then move the code you have in your destructor to your Dispose method – taquion Jun 02 '19 at 13:35
-
Thanks for the reply. IDisposable is implemented but appears not be called. Not exactly sure why. Either the external apps don't call it or maybe because the class calls a singleton and somehow that's keeping it from running. It's ancient code and GC.SuppressFinalize(this); is being used in Dispose; I did try moving the destructor code there to test but it never decrements my count. – Jun 02 '19 at 14:24
-
The simple and uncomfortable answer is that you should not use the garbage collector to manage non-memory resources like a UI. – Holger Jun 03 '19 at 12:40
-
If I could but not my decision made some 30 years ago. Now hundreds of apps and drivers are hooked and without another solution i'm stuck as long as COM is around and can't delete a specific reference from the GC. Maybe someday. – Jun 03 '19 at 22:04
-
C# is not that old. Neither does it sound believable that “hundreds of apps and drivers” depend on the way how you manage your UI. – Holger Jun 04 '19 at 09:33
-
okay, you made me look, the standard was created in 1998. https://ascom-standards.org/About/HowWorks.htm – Jun 05 '19 at 10:40