懒惰< T>:"功能评价,需要所有的线程运行" [英] Lazy<T>: "The function evaluation requires all threads to run"

查看:558
本文介绍了懒惰< T>:"功能评价,需要所有的线程运行"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些静态属性静态类。我初始化所有的人都在一个静态构造函数,但后来意识到,这是一种浪费,并在需要的时候,我应该延迟加载每个属性。所以,我切换到使用 System.Lazy< T> 键入做最脏最累的工作,并告诉它不使用任何的它的线程安全功能,因为在我的情况执行总是单线程的。

I have a static class with some static properties. I initialized all of them in a static constructor, but then realized that it is wasteful and I should lazy-load each property when needed. So I switched to using the System.Lazy<T> type to do all the dirty work, and told it to not to use any of its thread safety features since in my case execution was always single threaded.

我结束了以下类:

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; } }
}

在调试时,我发现我从来没有见过一个消息:

When debugging, I noticed a message I've never seen:

功能评估需要所有的线程运行

The function evaluation requires all threads to run

使用前懒&LT; T&GT; ,直接显示的值。现在,我需要在圆形按钮单击带有图标的线程来评估懒值。这种情况只有在我的属性被检索 .value的懒&LT; T&GT; 。当扩大的实际懒&LT的调试器可视节点; T&GT; 对象时,<​​code>值属性只是显示无效,没有任何消息。

Before using Lazy<T>, the values were displayed directly. Now, I need to click on the round button with the threads icon to evaluate the lazy value. This happens only on my properties that are retrieving the .Value of Lazy<T>. When expanding the debugger visualizer node of the actual Lazy<T> object, the Value property simply displays null, without any message.

那有什么意思的消息,为什么它是在我的情况显示?

推荐答案

我发现一个名为MSDN页面的如何:刷新观察值解释它:

I've found an MSDN page titled "How to: Refresh Watch Values" explaining it:

当您评估在调试器的前pression的两个刷新图标中的一个可能出现在值列。一刷新图标是包含两个箭头,其以相反的方向绕一个圆。另一种是包含类似于线程2波浪线的圆。

When you evaluate an expression in the debugger, one of two refresh icons might appear in the Value column. One refresh icon is a circle that contains two arrows, which circle in opposite directions. The other is a circle that contains two wavy lines that resemble threads.

...

如果出现两个线程,前pression是不是因为一个潜在的跨线程依赖的评估。跨线程依赖意味着,评估code需要其他线程在应用程序中暂时运行。:当您处于中断模式,在应用程序中的所有线程通常被停止。从而允许其他线程临时运行可以对你的程序的状态意想不到的效果,并使得调试器忽略的事件,如断点。

If the two threads appear, the expression was not evaluated because of a potential cross-thread dependency. A cross-thread dependency means that evaluating the code requires other threads in your application to run temporarily. When you are in break mode, all threads in your application are typically stopped. Allowing other threads to run temporarily can have unexpected effects on the state of your program and causes the debugger to ignore events such as breakpoints.

我还是想一个更好的解释,如果任何人都可以给它。这不回答的问题包括:什么样的评价要求所有的线程来运行?如何调试器识别这样的情况?当您单击到底发生了什么线程刷新图标?

I'd still like a better explanation if anyone can give it. Questions that this doesn't answer include: What kind of evaluation requires all threads to run? How does the debugger identify such a case? What exactly happens when you click the thread refresh icon?

编辑:我想检查懒&LT当我通过这个答案跌跌撞撞; T&GT; 下的ILSpy (对于一个完全不同的原因)。在值的getter 酒店有一个 Debugger.NotifyOfCrossThreadDependency()的调​​用。 MSDN有这样一段话:

I think I've stumbled across the answer when examining Lazy<T> under ILSpy (for a completely different reason). The getter of the Value property has a call to a Debugger.NotifyOfCrossThreadDependency(). MSDN has this to say:

...]执行功能评价通常需要冷冻的所有主题除了正在执行的评估的线程。如果功能评价,需要在多个线程执行,如可能发生在远程方案,评估将阻塞。该NotifyOfCrossThreadDependency通知告知,它已经释放一个线程或中止功能评价调试器。

[...] performing a function evaluation typically requires freezing all threads except for the thread that is performing the evaluation. If the function evaluation requires execution on more than one thread, as might occur in remoting scenarios, the evaluation will block. The NotifyOfCrossThreadDependency notification informs the debugger that it has to release a thread or abort the function evaluation.

所以基本上,以prevent在您尝试评估一些前pression和Visual Studio的只是挂起30秒,然后通知你,一个功能评价已超时恼人的情况下,code有机会告知必须解冻其他线程的评估调试成功或以其他方式评估将永远阻塞。

So basically, to prevent the annoying case where you try to evaluate some expression and Visual Studio just hangs for 30 seconds and then informs you that "a function evaluation has timed out", the code has a chance to inform the debugger that it must unfreeze other threads for the evaluation to succeed or otherwise the evaluation will block forever.

由于运行其它线程可能会破坏你的调试会话,因为通常当你评估一个前pression其他所有线程都保持冻结,调试器不会自动proceeed,让你跳下去了兔子洞前发出警告。

Since running other threads may disrupt your debugging session, as usually when you evaluate an expression all other threads are kept frozen, the debugger doesn't automatically proceeed and warns you before letting you jump down the rabbit hole.

这篇关于懒惰&LT; T&GT;:&QUOT;功能评价,需要所有的线程运行&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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