private void Button_Click(object sender, RoutedEventArgs e) { var bgWorker = new BackgroundWorker { WorkerReportsProgress = true }; List <SpeedList> mItems = SpeedListView.Items.Cast <SpeedList>().ToList(); IsEnabled = false; SpeedListView.Items.Clear(); bgWorker.DoWork += (o, args) => { int i = 0; 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 = "PASS", Asn = IpTools.GeoIpLocal(item.Server) }); continue; } if (TypeDNS) { delayTime = Ping.MPing(item.Server).Average(); if (delayTime == 0) { delayTime = Ping.Tcping(item.Server, 53).Average(); } } else { delayTime = Ping.Curl(ListStrings[i], "auroradns.github.io").Average(); } bgWorker.ReportProgress(i++, new SpeedList { Server = item.Server, Name = item.Name, DelayTime = delayTime.ToString("0ms"), Asn = IpTools.GeoIpLocal(item.Server) }); } }; bgWorker.ProgressChanged += (o, args) => { SpeedListView.Items.Add((SpeedList)args.UserState); }; bgWorker.RunWorkerCompleted += (o, args) => { IsEnabled = true; }; bgWorker.RunWorkerAsync(); }
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) }); continue; } if (TypeDNS) { delayTime = Ping.MPing(item.Server).Average(); if (delayTime == 0) { delayTime = Ping.Tcping(item.Server, 53).Average(); } } else { delayTime = Ping.Curl(ListStrings[i].Split('*', ',')[0].Trim(), "github.io").Average(); } bgWorker.ReportProgress(i++, new SpeedList { Server = item.Server, Name = item.Name, DelayTime = Convert.ToInt32(delayTime), Asn = IpTools.GeoIpLocal(item.Server) }); } }; 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(); }