public void ChangeVpn(Vpn_ vpn) { _selectedVpn = vpn; BitmapImage bitmapImage = new BitmapImage(); flags.TryGetValue(vpn.Country, out bitmapImage); SelectedRegionFlag.Source = bitmapImage; SelectadRegionText.Content = vpn.Country; }
private void getVpnAsync() { BackgroundWorker bw = new BackgroundWorker(); bw.RunWorkerCompleted += delegate { LoadingAnim(false); ChangeVpn(VpnList._VpnList.First()); foreach (Vpn_ vpn in VpnList._VpnList) { ServerItem serverItem = new ServerItem(); serverItem.NameOf.Content = vpn.Country; BitmapImage bitmapImage = new BitmapImage(); flags.TryGetValue(vpn.Country, out bitmapImage); serverItem.Flag.Source = bitmapImage; serverItem.vpn = vpn; long repl = 0; BackgroundWorker bw2 = new BackgroundWorker(); bw2.RunWorkerCompleted += delegate { if (repl > 120) { Brush brush = Brushes.Red; serverItem.Ping.Foreground = brush; } if (repl < 120 & repl > 80) { Brush brush = Brushes.Yellow; serverItem.Ping.Foreground = brush; } if (repl < 80) { Brush brush = Brushes.LimeGreen; serverItem.Ping.Foreground = brush; } serverItem.Ping.Content = repl; if (repl > 0) { serverItem.PreviewMouseDown += delegate { setSerstatus(false); ChangeVpn(serverItem.vpn); }; StackServers.Children.Add(serverItem); } }; bw2.DoWork += delegate { Ping ping = new System.Net.NetworkInformation.Ping(); PingReply pingReply = null; pingReply = ping.Send(vpn.IP); repl = pingReply.RoundtripTime / 2; }; bw2.RunWorkerAsync(); } }; bw.DoWork += delegate { for (int i = 0; i < 7; i++) { try { string st = new WebClient() { Proxy = null }.DownloadString(VpnServers[i][0]); st = st.Remove(st.IndexOf("<h3>L2TP/IPSec (PSK)</h3>"), st.Length - st.IndexOf("<h3>L2TP/IPSec (PSK)</h3>", 10)); st = st.Remove(0, st.IndexOf("<h3>PPTP</h3>")); string[] s = new string[3]; int k2 = 0; while (st.Contains("</b>")) { string k = st.Remove(0, st.IndexOf("</b>") + 5); k = k.Remove(k.IndexOf("</li>"), k.Length - k.IndexOf("</li>")); st = st.Remove(0, st.IndexOf("</li>") + 5); s[k2] = k; k2++; } Vpn_ vpn = new Vpn_ { IP = s[0], User = s[1], Pass = s[2], Country = VpnServers[i][1] }; VpnList._VpnList.Add(vpn); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }; bw.RunWorkerAsync(); }