I am indifferent if you are a VB.NET or C# or other .NET developer. Answers would be appreciated no matter what language. Really looking for an idea…
I have a FACTORY class like the following:
Public Class MyFactory
Public Shared theStuffICreated as List(Of Stuff)
Public Shared/Static Sub Create(...)
...
End Sub
Public Shared/Static Sub ThingsToDoWhenClassIsDestroyed()
...
End Sub
End Class
How can I make sure that ThingsToDoWhenClassIsDestroyed() is called when this class is “Destroyed” by the finalizer. I understand that since everything is shared/static it never really was created, but isn’t there some footprint of it somewhere that needs to be destroyed? Assume this class can be used by multiple GUI and console applications and it is not feasible to make a call to MyFactory.ThingsToDoWhenClassIsDestroyed() in each GUI’s Dispose method. Any ideas?
Application.ApplicationExit was a good start. AppDomain.CurrentDomain.UnloadDomain was a good start also but neither worked all the time. Application.ApplicationExit seems to work for GUI apps but not for console apps. I ended up using AppDomain.CurrentDomain.ProcessExit because that works for everything. So my code looks like this: