有人可以解释一下为什么这段代码不起作用:
...
foreach (VisualChunk chunk in SortedChunks)
{
System.Action a = () => MyFunction_Threaded(chunk);
Console.Write("Synchronous : ");
a.Invoke();
System.Threading.Tasks.Task.Factory.StartNew(
a,
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.Default)
}
...
private void MyFunction_Threaded(VisualChunk chunk)
{
Console.WriteLine(chunk.ChunkID);
}
它在控制台给我以下输出:
Synchronous : 0
Synchronous : 1
Synchronous : 2
Synchronous : 3
Synchronous : 4
Synchronous : 4294967291
Synchronous : 4294967292
Synchronous : 4294967293
Synchronous : 4294967294
Synchronous : 4294967295
Synchronous : 4294967296
Synchronous : 4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
Synchronous : 4294967298
4294967298
事实上,当安装 .net framework 4.5(win 8 或 VS2012)时,这段代码运行良好。当只安装 4.0 时,这个问题就出现了!