这是一个简单的.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