【发布时间】:2013-09-24 14:28:33
【问题描述】:
我有这个相当简单的逻辑:
class Program
{
static void Main(string[] args)
{
using (TransactionScope ts = new TransactionScope())
{
System.Threading.Tasks.Parallel.Invoke(() =>
{
TransactionScope y = ts;
System.Diagnostics.Debug.WriteLine("Test");
},
() =>
{
System.Diagnostics.Debug.WriteLine("Test");
}
);
ts.Complete();
}
}
}
如果您在两个Debug.WriteLine() 语句上放置断点,您会注意到当它在第一个语句上中断时,y 和ts 都被调试器列为本地变量。但是当它在后者中遇到断点时,ts 不会被列为本地,此外,将ts 添加到监视窗口会得到The name 'ts' does not exist in the current context.
这个变量是实际捕获还是其他机制?我查看了有关变量捕获的文章,但找不到任何明确说明变量仅在使用时才被捕获的内容,但我假设它被称为变量捕获,因为它只“捕获”什么它需要并且不保留对所有可用内容的引用。
【问题讨论】:
-
我认为编写的代码可能会在可能的并行操作完成之前调用
Complete。 -
@Kit,来自 Parallel.Invoke 的文档:
This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination. -
哦,呵呵。我忘记了这个。谢谢提醒。
标签: c# scope anonymous-methods