自己的demo

 for (int i = ; i < listBoxDevices.Items.Count; i++)
{
var tempDeviceId = listBoxDevices.Items[i].ToString();
if (tempDeviceId.Contains("(掉线)"))
{
var id = GlobalConvert.GetDeviceID(tempDeviceId.Replace("(掉线)", string.Empty));
if (id == reonlineId)
{
listBoxDevices.Invoke((MethodInvoker)delegate
{
try
{
listBoxDevices.Items[i] = GlobalConvert.GetDeviceID(reonlineId);
}
catch (Exception ex)
{
ExceptionLog.Instance.WriteLog(ex, LogType.UI);
}
});
}
}
}

代码里面有一个for循环

循环内部调用了匿名委托

模拟了一下:

但是下面这段代码的执行,不会出现问题是正常的

  private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Closure);
thread.IsBackground = true;
thread.Start();
} private void Closure()
{
for (int i = ; i < ; i++)
{
this.Invoke((MethodInvoker) delegate
{
textBox1.AppendText(string.Format("{0}:{1}{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), i, Environment.NewLine));
});
}
}

What are 'closures' in .NET?

I have an article on this very topic. (It has lots of examples.)

In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing.

The general feature of closures is implemented in C# by anonymous methods and lambda expressions.

Here's an example using an anonymous method:

using System;

class Test
{
static void Main()
{
Action action = CreateAction();
action();
action();
} static Action CreateAction()
{
int counter = 0;
return delegate
{
// Yes, it could be done in one statement;
// but it is clearer like this.
counter++;
Console.WriteLine("counter={0}", counter);
};
}
}

Output:

counter=1
counter=2

Here we can see that the action returned by CreateAction still has access to the counter variable, and can indeed increment it, even though CreateAction itself has finished.

References:

http://stackoverflow.com/questions/595482/what-are-closures-in-c

closure in C# takes the form of an in-line delegate/anonymous method.

closure is attached to its parent method meaning that variables defined in parent's method body can be referenced from within the anonymous method.

http://csharpindepth.com/Articles/Chapter5/Closures.aspx

http://csharpindepth.com/Articles/Closures

C# 闭包问题-你被”坑“过吗?

access to modified closure 闭包的问题的更多相关文章

  1. iOS - Swift Closure 闭包

    1.Closure 闭包在 Swift 中非常有用.通俗的解释就是一个 Int 类型里存储着一个整数,一个 String 类型包含着一串字符,同样,闭包是一个包含着函数的类型.有了闭包,你就可以处理很 ...

  2. PHP Closure(闭包)类详解

    Closure 面向对象变成语言代码的复用主要采用继承来实现,而函数的复用,就是通过闭包来实现.这就是闭包的设计初衷. 注:PHP里面闭包函数是为了复用函数而设计的语言特性,如果在闭包函数里面访问指定 ...

  3. javascript closure 闭包 事件绑定

    先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...

  4. JS Closure 闭包

    /*一.变量的作用域要理解闭包,首先必须理解Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. ...

  5. Closure闭包示例

    var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); conso ...

  6. Php5.3的lambda函数以及closure(闭包)

    从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...

  7. ES3之closure ( 闭包 )

    词法作用域中使用的域,是变量在代码中声明的位置所决定的.嵌套的函数可以访问在其外部声明的变量. 闭包是函数和声明该函数的词法环境的组合. 1 创建单个闭包 JavaScript中的函数会形成闭包. 闭 ...

  8. modern php closure 闭包

    * 在array_map()函数中使用闭包 <?php $numbersPlusOne = array_map(function($number) { return $number + 1; } ...

  9. MapReduce的C#实现及单元测试(试验)

    MapReduce.cs类文件代码  MapReduce的执行方法 using System; using System.Collections.Generic; //using System.Lin ...

随机推荐

  1. CSS3滤镜!!!

    <!DOCTYPE html> <html> <head> <style> img { width: 33%; height: auto; float: ...

  2. [Guava官方文档翻译] 4. 使用Guava Ordering排序 (Ordering Explained)

    本文地址:http://www.cnblogs.com/hamhog/p/3537233.html 示例 assertTrue(byLengthOrdering.reverse().isOrdered ...

  3. php configure help

    `configure' configures this package to adapt to many kinds of systems. Usage: ./configure [OPTION].. ...

  4. linux 配置Socks51

    VPN大家耳熟能详,但是socks用到的人比较少,那什么是socks呢?请看第二段或者百度百科,socks分别有4和5两个版本,现在5为主流.工作中经常用VPN访问国外,但是同时国内的速度又慢了,让人 ...

  5. [数据库连接字符串] Access 连接字符串

    [数据库连接字符串] Access 连接字符串 //ODBC 标准安全策略 Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb ...

  6. 安装Android Studio报failed to find java version for 'C:\windows\system32\java.exe':[2] The system cannot find the specified file.错误的解决方案

    方案很简单,找到SYSTEM32目录下的java.exe文件,重命名为java.exe.orj. 方案出处:http://stackoverflow.com/questions/10339679/an ...

  7. Django操作数据库

    引入models的定义 from app.models import  myclass class  myclass():      aa =  models. CharField (max_leng ...

  8. ubuntu u盘安装

    参考博客:http://www.bjwilly.com/archives/325.html 1.下载光盘映像 目前可选12.04LTS(长期支持版本) http://www.ubuntu.org.cn ...

  9. 【git】切换分支获取代码

    Welcome to Git (version 1.9.5-preview20150319) Run 'git help git' to display the help index.Run 'git ...

  10. 更改Keil工程名

    假设原工程名为A,需要改成B. 1, 在工程目录下,把A.vuopt和A.uvproj改成B.uvopt和B.uvproj. 2,删除其他A文件. 3,打开工程B.然后修改下面位置: Project ...