Parallel.For
使用 Parallel.For 迴圈來 ping 給定網站 URL 陣列的示例。
static void Main()
{
string [] urls =
{
"www.stackoverflow.com",
"www.google.net",
"www.facebook.com",
"www.twitter.com"
};
System.Threading.Tasks.Parallel.For(0, urls.Length, i =>
{
var ping = new System.Net.NetworkInformation.Ping();
var result = ping.Send(urls[i]);
if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
{
Console.WriteLine(string.Format("{0} is online", urls[i]));
}
});
}