Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a ThreadStatic IDisposable be automatically disposed?

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?

like image 276
tigger Avatar asked Jan 11 '11 14:01

tigger


1 Answers

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.

like image 158
LukeH Avatar answered Oct 12 '22 23:10

LukeH