/// <summary> /// The background task to refresh all the servers /// </summary> void RefreshServerList(object sender, DoWorkEventArgs e) { WebClient client = new WebClient(); foreach (string s in this.serverManager.GetServers()) { //Holds the webresponse string result = null; //Init a new game list for the server ServerGameList serverGameList = new ServerGameList(); serverGameList.url = s; serverGameList.dirs = new List<string>(); serverGameList.status = "Online"; try { result = client.DownloadString(s); string[] dirs = result.Split('\0'); if (dirs.Length == 0) serverGameList.status = "Error, not a lol server"; else { if (dirs[0] != "lol.dirlist") serverGameList.status = "Error, not a lol server"; else { //Add all dirs for (int i = 1; i < dirs.Length; i++) if (dirs[i].Length == 0) continue; else serverGameList.dirs.Add(dirs[i]); } } } catch (Exception ex) { serverGameList.status = ex.Message; } finally { //Find the url to remove ServerGameList? itemToRemove = null; foreach (ServerGameList list in this.serverList) { if (list.url == s) { itemToRemove = list; break; } } serverList.Remove(itemToRemove.Value); serverList.Add(serverGameList); } } }
/// <summary> /// Refreshes the ServerList, when it is finished, executes the whenDone delegate /// </summary> /// <param name="whenDone">Delegate to excecute when serverlist is refreshed</param> void RefreshServers(RunWorkerCompletedEventHandler whenDone) { if (bgWorker.IsBusy) { MessageBox.Show("Error, an other process is already running"); return; } bgWorker = new BackgroundWorker(); bgWorker.DoWork += new DoWorkEventHandler(RefreshServerList); bgWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs args) { textBoxLog.Text += "\nRefreshing server list completed!"; }; //If whendone is set, add an eventhandler if (whenDone != null) bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(whenDone); //Empty all rows serverList.Clear(); foreach (string url in this.serverManager.GetServers()) { ServerGameList list = new ServerGameList(); list.url = url; list.status = "Waiting for Update..."; serverList.Add(list); } bgWorker.RunWorkerAsync(); textBoxLog.Text += "\nStarted refreshing server list..."; }