access to modified closure 闭包的问题
自己的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
A closure in C# takes the form of an in-line delegate/anonymous method.
A 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
access to modified closure 闭包的问题的更多相关文章
- iOS - Swift Closure 闭包
1.Closure 闭包在 Swift 中非常有用.通俗的解释就是一个 Int 类型里存储着一个整数,一个 String 类型包含着一串字符,同样,闭包是一个包含着函数的类型.有了闭包,你就可以处理很 ...
- PHP Closure(闭包)类详解
Closure 面向对象变成语言代码的复用主要采用继承来实现,而函数的复用,就是通过闭包来实现.这就是闭包的设计初衷. 注:PHP里面闭包函数是为了复用函数而设计的语言特性,如果在闭包函数里面访问指定 ...
- javascript closure 闭包 事件绑定
先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...
- JS Closure 闭包
/*一.变量的作用域要理解闭包,首先必须理解Javascript特殊的变量作用域.变量的作用域无非就是两种:全局变量和局部变量.Javascript语言的特殊之处,就在于函数内部可以直接读取全局变量. ...
- Closure闭包示例
var foo = function(){ var cnt = 0; return function(){ return cnt++; }; }; var closure = foo(); conso ...
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- ES3之closure ( 闭包 )
词法作用域中使用的域,是变量在代码中声明的位置所决定的.嵌套的函数可以访问在其外部声明的变量. 闭包是函数和声明该函数的词法环境的组合. 1 创建单个闭包 JavaScript中的函数会形成闭包. 闭 ...
- modern php closure 闭包
* 在array_map()函数中使用闭包 <?php $numbersPlusOne = array_map(function($number) { return $number + 1; } ...
- MapReduce的C#实现及单元测试(试验)
MapReduce.cs类文件代码 MapReduce的执行方法 using System; using System.Collections.Generic; //using System.Lin ...
随机推荐
- ###C中的extern-static-const关键词
#@date: 2014-06-14 #@author: gerui #@email: forgerui@gmail.com Contents extern的作用一般是用来声音一个外部变量和函数.一般 ...
- 转:EF调用存储过程、函数
EF调用存储过程.函数 2014-04-02 09:12:20| 分类: ORM框架|举报|字号 订阅 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- ASP.net后台弹出消息对话框的方法!【转】
在winform后台,我们通过MessageBox.show(“消息")的方式来返回后台信息,在webform后台,我们通过Response.write(”消息")来返 ...
- C语言使用中的细节问题总结
1.在使用bool关键字时,出现"error:'bool' undeclared(first use in this function)"的错误,原因为C语言本身是没有bool关键 ...
- HTML5:基本使用
单行文本输入框 type为text表示input元素为一个单行文本框,是input元素的默认表现形式.单行文本输入框支持下面的属性设置. A:设定元素大小 maxlength属性设定用户能够输入的字符 ...
- 免费使用的图表控件XML/SWF Charts 5.08
免费使用的图表控件XML/SWF Charts 5.08 http://www.pin5i.com/showtopic-26053.html 10个免费的在线统计图表工具 http://paranim ...
- python初准备:安装easy_install和pip
安装easy_install wget http://peak.telecommunity.com/dist/ez_setup.py python ez_setup.py 安装pip wget htt ...
- 【WPF】路由事件
总结WPF中的路由事件,我将学到的内容分为四部分来逐渐掌握 第一部分:wpf中内置的路由事件 以Button的Click事件来说明内置路由事件的使用 XAML代码: <Window x:Clas ...
- pymssql 安装测试
平台 : windows 7 32位 数据库 : SQLSERVER 2008 python 2.7 & pymssql模块 数据库和python 等模块安装说明省略 以下贴出测试代码: 单 ...