static class Program
{ /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
List<tcc> tcclist=new List<tcc>();
for(int i=0;i<10;i++){
tcclist.Add(new tcc{name="tcc"+i});
}
foreach(tcc c in tcclist)
{
c.endE+=new tcc.endEvent(delegate()
{
Console.WriteLine(c.name + "------end");
});
Thread t = new Thread(delegate()
{ Console.WriteLine(c.name+"-----go");
c.cfun(); });
t.Start();
Thread.Sleep(1000);
} }
} public class tcc
{
public delegate void endEvent();
public event endEvent endE;
public string name = "";
public void cfun()
{
Thread.Sleep(100);
endE();
}
}
var k = i;
tcclist.Add(new tcc{name="tcc"+k});
}
记得有人说过.net4.5好像是处理了这个容易犯的错误,不知道是不是真的。因为我还没有使用 .net 4.5。