private void Button_Click(object sender, RoutedEventArgs e) { ProgressBar.Visibility = Visibility.Visible; var bgWorker = new BackgroundWorker { WorkerReportsProgress = true }; List <SpeedList> mItems = SpeedListView.Items.Cast <SpeedList>().ToList(); StratButton.IsEnabled = false; SpeedListView.Items.Clear(); bgWorker.DoWork += (o, args) => { int i = 1; foreach (SpeedList item in mItems) { double delayTime; if (item.Server.Contains("google.com") && !DnsSettings.ProxyEnable && IpTools.GeoIpLocal(MainWindow.IntIPAddr.ToString(), true).Contains("CN")) { bgWorker.ReportProgress(i++, new SpeedList { Server = item.Server, Name = item.Name, DelayTime = 0, Asn = IpTools.GeoIpLocal(item.Server).Trim() }); continue; } try { if (TypeDNS) { //delayTime = Ping.MPing(item.Server).Average(); //if (delayTime == 0) // delayTime = Ping.Tcping(item.Server, 53).Average(); //var dnsDelayTime = Ping.DnsTest(item.Server).Average(); //if (dnsDelayTime > delayTime) delayTime = dnsDelayTime; delayTime = Math.Round(Ping.DnsTest(item.Server).Average(), 2); } else { delayTime = Math.Round(Ping.Tcping(item.Server, 443).Average(), 2); } bgWorker.ReportProgress(i++, new SpeedList { Server = item.Server, Name = item.Name, DelayTime = delayTime, Asn = IpTools.GeoIpLocal(item.Server).Trim() }); } catch (Exception exception) { Console.WriteLine(exception); } } }; bgWorker.ProgressChanged += (o, args) => { SpeedListView.Items.Add((SpeedList)args.UserState); ProgressBar.Value = args.ProgressPercentage; }; bgWorker.RunWorkerCompleted += (o, args) => { StratButton.IsEnabled = true; ProgressBar.Value = 0; ProgressBar.Visibility = Visibility.Hidden; }; bgWorker.RunWorkerAsync(); }