可以自动处置ThreadStatic IDisposable吗? [英] Can a ThreadStatic IDisposable be automatically disposed?

查看:76
本文介绍了可以自动处置ThreadStatic IDisposable吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是如何自动调用dispose的问题-我的问题正好相反:
我有一个线程池,其中每个线程都有一个ThreadStatic图形(由Image创建)以执行文本大小测量。现在,我遇到了一个问题,即有时甚至由于读取TextRenderingHint属性也会失败而导致图形被丢弃(导致ArgumentException)。

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?

推荐答案

如果线程完全死掉,则 ThreadStatic <可以收集该线程的/ code>对象

If the thread dies completely then the ThreadStatic object for that thread can be collected.

GC不会调用 Dispose ,但是如果对象有一个后备终结器要在其自身之后进行清理,则GC应该在收集过程中的某个时候调用它。

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.

这篇关于可以自动处置ThreadStatic IDisposable吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆