【发布时间】:2014-12-28 07:06:34
【问题描述】:
当我真的不确定为什么时,我收到了ArgumentOutOfRangeException。
Task[] downloadTasks = new Task[music.Count];
for (int i = 0; i < music.Count; i++)
downloadTasks[i] = Task.Factory.StartNew(() => DownloadAudio(music[i], lstQueue.Items[i]));
Task.Factory.ContinueWhenAll(downloadTasks, (tasks) =>
{
MessageBox.Show("All the downloads have completed!",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
});
当for loop 在i = 1 时运行时会发生错误,当我确定music.Count = 1 时,我不确定它为什么会这样。
我一直尝试这种方法作为for loop 的替代方法,但遇到了同样的异常:
int index = 0;
foreach (MusicFile song in music)
{
downloadTasks[index] = Task.Factory.StartNew(() => DownloadAudio(song, lstQueue.Items[index]));
index++;
}
上面的代码中是否有任何可能导致这种情况的地方?
我也不确定这是否相关,但是当我可以毫无例外地使用线程完成同样的事情时。只有当我尝试执行任务时才会出现此异常。
【问题讨论】:
标签: c# .net task-parallel-library task