这是我的课程:
class QuizShow
{
List<Player> players;
Task<GameResult>[] games;
public QuizShow(List<Player> _players)
{
players = new List<Player>(_players);
games = new Task<GameResult>[players.Count];
}
public void StartQuiz()
{
for(int i = 0; i < players.Count; ++i)
{
games[i] = Task.Run(() => Game(players[i]));
}
}
public void WaitForEnd()
{
Task.WaitAll(games);
}
}
对我来说,我很难理解的是,执行
时,方法StartQuiz
上升了ArgumentOutOfRangeException
。
...
quiz.QuizStart();
quiz.WaitForEnd();
...
在Main中,而当我这样更改时:
public void StartQuiz()
{
for(int i = 0; i < players.Count; ++i)
{
var player = players[i];
games[i] = Task.Run(() => Game(player));
}
}
不会发生此问题。
方法Game
不会更改players
或games
。
此外,在调试时,只有当我不进入StartQuiz
并执行该方法时,问题才会发生。
感谢您的解释。