Parallel.ForEach

使用 Parallel.ForEach 循环 ping 给定网址 URL 数组的示例。

static void Main()
{
    string [] urls = 
    {
        "www.stackoverflow.com", 
        "www.google.net", 
        "www.facebook.com", 
        "www.twitter.com"
    };
    
    System.Threading.Tasks.Parallel.ForEach(urls, url =>
    {
        var ping = new System.Net.NetworkInformation.Ping();

        var result = ping.Send(url);

        if (result.Status == System.Net.NetworkInformation.IPStatus.Success)
        {
            Console.WriteLine(string.Format("{0} is online", url));
        }
    });
}