private void Worker() { do { List <IPAddress> addresses = null; int activeCount = -1; lock (_lock) { activeCount = active.Count; addresses = queue.Take(MaxSimultaneousRequests - activeCount).ToList(); active.AddRange(addresses); } if (addresses != null && addresses.Count > 0) { foreach (var address in addresses) { PingSent?.Invoke(address); var ping = new Ping(); lock (_lock) activeRequests.Add(ping); ping.PingCompleted += Ping_PingCompleted; ping.SendAsync(address, Timeout, address); // Remove from queue lock (_lock) queue.RemoveAll(o => o == address); } } } while (!stop.WaitOne(10, true) && Count > 0); }
private void PingQueue_PingSent(IPAddress address) { PingSent?.Invoke(this, address); }