我想知道是否有可能改进这段代码以获得更好的性能。我对服务器端的异步操作还不熟悉,所以请您耐心等待:
con.GetGame(id, game => {
foreach(Player p in game.Team1)
{
p.SomeExtraDetails = GetPlayerDetails(p.Id);
}
// I would like the player data to be set on all players
// before ending up here
});
private PlayerDetails GetPlayerDetails(double playerId)
{
var task = con.GetPlayer(playerId);
PlayerDetails ret = null;
Task continuation = task.ContinueWith(t =>
{
ret = t.Result;
});
continuation.Wait();
return ret;
}
如果我理解正确,
continuation.Wait();
会阻塞主线程。有没有办法让任务同时运行?
Task
?我认为如果你保持一致会更好。 - svick