【发布时间】:2011-05-26 12:04:18
【问题描述】:
我有一个带有一些静态属性的静态类。我在一个静态构造函数中初始化了所有这些,但后来意识到这很浪费,我应该在需要时延迟加载每个属性。所以我转而使用System.Lazy<T> 类型来完成所有脏活,并告诉它不要使用它的任何线程安全功能,因为在我的例子中执行总是单线程的。
我最终上了以下课程:
public static class Queues
{
private static readonly Lazy<Queue> g_Parser = new Lazy<Queue>(() => new Queue(Config.ParserQueueName), false);
private static readonly Lazy<Queue> g_Distributor = new Lazy<Queue>(() => new Queue(Config.DistributorQueueName), false);
private static readonly Lazy<Queue> g_ConsumerAdapter = new Lazy<Queue>(() => new Queue(Config.ConsumerAdaptorQueueName), false);
public static Queue Parser { get { return g_Parser.Value; } }
public static Queue Distributor { get { return g_Distributor.Value; } }
public static Queue ConsumerAdapter { get { return g_ConsumerAdapter.Value; } }
}
在调试时,我注意到一条我从未见过的消息:
函数求值需要所有线程运行
在使用Lazy<T>之前,值是直接显示的。现在,我需要单击带有线程图标的圆形按钮来评估惰性值。这只发生在我正在检索.Value 的Lazy<T> 的属性上。展开实际 Lazy<T> 对象的调试器可视化节点时,Value 属性仅显示 null,没有任何消息。
该消息是什么意思,为什么在我的案例中显示?
【问题讨论】:
标签: c# visual-studio visual-studio-2010 lazy-loading visual-studio-debugging