這是一個簡單的.NET 4應用程序。這是我要運行的代碼:在.NET 4中異步運行方法的簡單方法是什麼?
string username = "userfoo";
string password = "passwordfoo";
for (int i = 0; i < 2000; i++)
{
uint matchId;
if (!uint.TryParse(i.ToString(), out matchId))
{
Console.WriteLine("Invalid Match ID!");
return;
}
Client client = new Client (username, password, matchId);
// connect
client.Connect();
client.Wait();
if (client.Match != null)
{
Console.WriteLine("Inserting match: #{0}", client.Match.match_id);
Helpers.MatchHelper.AddMatchToDatabase(client.Match);
}
else
{
Console.WriteLine("Couldn't get match: #{0}", 1);
}
}
而不是做這一個接一個(它會永遠需要 - 根據我的計算415天不停),什麼是調用的每次迭代的最簡單方法這是for循環異步?
大多數問題和文章都很舊(大約在2001年!)肯定必須有更現代化的方法嗎?
http://msdn.microsoft.com/en-us/magazine/cc301332.aspx
你有沒有考慮過使用任務? – 2012-07-18 17:51:32