问题
This is not a question of how to automatically call dispose - my problem is the opposite: I have a thread pool where each thread has a ThreadStatic Graphics (which was created from an Image) to perform text size measuring. Now I've ran into the problem that from time to time the graphics seems to be disposed as even reading the TextRenderingHint property fails(causes an ArgumentException).
Is there some mechanism which disposes the Graphics e.g. if the thread is idle for a long period?
回答1:
If the thread dies completely then the ThreadStatic
object for that thread can be collected.
The GC won't call Dispose
directly, but if the object has a "fallback" finaliser to clean-up after itself then the GC should call that at some point during the collection process.
来源:https://stackoverflow.com/questions/4658934/can-a-threadstatic-idisposable-be-automatically-disposed