我想知道是否有可能改进此代码以获得更好的性能。我是新来的服务器端全异步的东西,所以请容忍我在这里:嵌套异步任务
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();
块主线程。
有什么办法可以让任务同时运行吗?
你可以使用C#5.0吗?另外,为什么你的一些方法使用continuations和一些'Task's?我认为如果你一致的话会更好。 – svick
@svick我正在使用VS2012,但我相信它是.NET 4.5 atm。 – Johan
@Johan它不是一个错字 - 它是C#5(语言)和.NET 4.5(框架);) –