private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { int i = MAXItems - 1; if (currType == TypeOfGraph.RealSimulation) { while (true) { try { ExtendedPing ping = new ExtendedPing(i); ping.On_ExtendedPing_Completed += new ExtendedPing.ExtendedPing_Completed( (obj, PingCompletedEventArgs, ident) => { if (backgroundWorker.CancellationPending) { return; } pingReplies[i] = PingCompletedEventArgs.Reply; PackageValues[i].Value = PingCompletedEventArgs.Reply.Status == IPStatus.Success ? 1 : 0; double sumTime = pingReplies.Sum(v => (double)(v?.Status == IPStatus.Success ? (v?.RoundtripTime ?? 0) : 0)); //double sumBytes = pingReplies.Sum(v => v.Buffer.Length); InSpeedValues[i].Value = sumTime / pingReplies.Count(v => v != null);//sumTime > 0 ? sumBytes / sumTime : 0; if (obj is Ping p) { p.Dispose(); } }); await ping.SendPingAsync(ip, 1000); Thread.Sleep(500); } catch (Exception ex) { LoggingHelper.LogEntry(SystemCategories.GeneralError, $"{ex.Message} {ex.StackTrace}"); } --i; if (i < 0) { i = MAXItems - 1; } if (backgroundWorker.CancellationPending) { e.Cancel = true; return; } } } else if (currType == TypeOfGraph.GNS3Simulation) { WebSocket ws = null; var wait = TimeSpan.FromSeconds(3); using (ws = new WebSocket($"ws://{ip}")) { Notification notify; ws.WaitTime = wait; ws.OnMessage += (s, ev) => { if (ev.Data.Contains("ping")) { notify = JsonConvert.DeserializeObject <Notification>(ev.Data); if (notify.Event is PingEvent pe) { PackageValues[i].Value = pe.RAMLoad; InSpeedValues[i].Value = pe.CPULoad; --i; if (i < 0) { i = MAXItems - 1; } } } }; ws.Connect(); while (true) { if (backgroundWorker.CancellationPending) { e.Cancel = true; return; } Thread.Sleep(wait); } } } }
private void Ping(string host, int attempts, int timeout, CancellationToken ct) { List <Thread> threads = new List <Thread>(); ConcurrentBag <KeyValuePair <ExtendedPing, PingCompletedEventArgs> > pings = new ConcurrentBag <KeyValuePair <ExtendedPing, PingCompletedEventArgs> >(); try { int counter = 0; SetTextLabel2($"Pinging: {host}"); for (int i = 0; i < attempts; i++) { var th = new Thread(delegate() { try { ExtendedPing ping = new ExtendedPing(i); ping.PingCompleted += new PingCompletedEventHandler((obj, PingCompletedEventArgs) => { pings.Add(new KeyValuePair <ExtendedPing, PingCompletedEventArgs>((ExtendedPing)obj, PingCompletedEventArgs)); Interlocked.Decrement(ref counter); }); ping.SendAsync(host, timeout, host); } catch (Exception ex) { LoggingHelper.LogEntry(SystemCategories.GeneralError, $"{ex.Message} {ex.StackTrace}"); } }); th.Start(); threads.Add(th); Interlocked.Increment(ref counter); } while (Interlocked.CompareExchange(ref counter, 0, 0) > 0 && !ct.IsCancellationRequested) { Thread.Sleep(100); } if (!ct.IsCancellationRequested) { var status = pings.All(p => p.Value.Reply.Status == IPStatus.Success) ? Status.Up : pings.Any(p => p.Value.Reply.Status == IPStatus.Success) ? Status.Unstable : Status.Down; UpdateProgress(UpdateProgressBar, SetTextLabel2, $"Status: {host} - {status}"); if (status != Status.Down) { Interlocked.Increment(ref foundDevices); string hostname = GetHostName(host); string macaddres = GetMacAddress(host); AddDevice(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("IP", host), new KeyValuePair <string, string>("Hostname", hostname), new KeyValuePair <string, string>("Macaddress", macaddres), new KeyValuePair <string, string>("Status", status.ToString()), }); } } ct.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { } finally { if (ct.IsCancellationRequested) { for (int i = 0; i < threads.Count; ++i) { threads[i].Abort(); } } foreach (var ping in pings) { ping.Key.Dispose(); } } }