Can a ThreadStatic IDisposable be automatically disposed?
Asked Answered
M

1

5

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?

Machicolation answered 11/1, 2011 at 14:57 Comment(0)
S
10

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.

Salvador answered 11/1, 2011 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.