using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
private static readonly SemaphoreSlim _sem = new SemaphoreSlim(3, 3);
private static readonly Random _r = new Random();
private static long _counter = 0;
public static IPAddress RandomIpAddress()
return new IPAddress(data);
public static async Task<PingReply> SomethingAsync(IPAddress address)
Interlocked.Increment(ref _counter);
var result = await ping.SendPingAsync(address, 1000);
Console.WriteLine($"{Interlocked.Read(ref _counter)} : {address} - {result.Status} {result.RoundtripTime}ms");
Console.WriteLine($"{Interlocked.Read(ref _counter)} : {address} - {ex.Message}");
Interlocked.Decrement(ref _counter);
public static async Task Test1(IEnumerable<IPAddress> list)
static async Task<PingReply> Process(IPAddress address)
return await SomethingAsync(address);
await Task.WhenAll(list.Select(Process));
public static async Task Test2(IEnumerable<IPAddress> list)
var block = new ActionBlock<IPAddress>(SomethingAsync, new ExecutionDataflowBlockOptions(){MaxDegreeOfParallelism = 3});
await Task.WhenAll(list.Select(x => block.SendAsync(x)));
var list = Enumerable.Range(0, 12).Select(x => RandomIpAddress()).ToList();
Console.WriteLine("---------");