I have a wellknown singleton service object, which accesses unmanaged
resources. It gets the handles to these resources in its constructor. I
want to free these resources when the lease expires (as opposed to at
the time of garbage collection), for two reasons
1. When more requests come, I need to reuse the unmanaged resources,
which I can't until the uncollected instance releases them (i.e. until
finalizer gets called by gc)
2. Why block resources unnecessarily? (This is kind of related to 1).
Now I couldn't find any "lease-expire" event, so two solutions come to
my mind.
1. Get a ridiculously long lease. Problem here is that those resources
remain occupied/blocked until the process terminates.
2. Call GC.Collect(MaxGeneration) in the constructor of the service
object and "hope" that the uncollected instance gets collected. Problem
here is HOPE.
Seems like a simple thing and I hope I'm missing something. Any
suggestions?