private void buttonDeleteAllOfficials_Click(object sender, EventArgs e) { var competition = listBoxCompetitions.SelectedItem as CompetitionWrapper; List <Task> tasks = new List <Task>(); foreach (var p in this.listBoxOfficials.Items.OfType <OfficialWrapper>()) { tasks.Add(new Task(new Action <object>(state => { OfficialWrapper pa = state as OfficialWrapper; this.apiClient.DeleteOfficial(pa.official.Id); lock (this.model.CompetitionOfficials) { this.model.CompetitionOfficials.Remove(pa); } }), p)); } if (tasks.Count == 0) { return; } Task.Factory.ContinueWhenAll(tasks.ToArray(), new Action <Task[]>(t => { MessageBox.Show("All officials deleted."); })); foreach (var t in tasks) { t.Start(); } }
private void LoadCompetitionOfficials() { this.model.CompetitionOfficials.Clear(); var competition = listBoxCompetitions.SelectedItem as CompetitionWrapper; var allOfficials = this.apiClient.GetOfficials(competition.competition.Id); if (allOfficials.Count == 0) { MessageBox.Show("No officials."); return; } List <Task> tasks = new List <Task>(); foreach (var p in allOfficials) { tasks.Add(new Task(new Action <object>(state => { var of = new OfficialWrapper(this.apiClient.GetOfficial((int)state)); lock (this.model.CompetitionOfficials) { this.model.CompetitionOfficials.Add(of); } }), p.Id)); } Task.Factory.ContinueWhenAll(tasks.ToArray(), new Action <Task[]>(t => { MessageBox.Show("All officials loaded."); })); foreach (var t in tasks) { t.Start(); } }
private void buttonFillWithOfficials_Click(object sender, EventArgs e) { var competition = listBoxCompetitions.SelectedItem as CompetitionWrapper; List <Task> tasks = new List <Task>(); Random random = new Random(); tasks.Add(new Task(new Action(() => { var chairman = this.model.Chairman[random.Next(0, this.model.Chairman.Count - 1)]; try { Uri chairmanUri = this.apiClient.SaveOfficial(new Api.Client.Models.OfficialDetail() { AdjudicatorChar = "CH", Task = "Chairman", CompetitionId = competition.competition.Id, Min = chairman.person.Min }); var ch = new OfficialWrapper(this.apiClient.Get <API_Models.OfficialDetail>(chairmanUri)); lock (this.model.CompetitionOfficials) { this.model.CompetitionOfficials.Add(ch); } } catch (Api.Client.Exceptions.ApiException ex) { this.errors.Add(ex.InnerException.Message); } }))); List <int> indexes = new List <int>(); for (int index = 0; index < 100; index++) { indexes.Add(random.Next(0, this.model.Adjudicator.Count - 1)); } foreach (int i in indexes.Distinct().Take(int.Parse(textBoxOfficialsCount.Text))) { tasks.Add(new Task(new Action <object>((state) => { var adjudicator = this.model.Adjudicator[(int)state]; try { Uri adjudicatorUri = this.apiClient.SaveOfficial(new Api.Client.Models.OfficialDetail() { AdjudicatorChar = adjudicator.person.Name.Substring(0, 2), Task = "Adjudicator", CompetitionId = competition.competition.Id, Min = adjudicator.person.Min }); var adj = new OfficialWrapper(this.apiClient.Get <API_Models.OfficialDetail>(adjudicatorUri)); lock (this.model.CompetitionOfficials) { this.model.CompetitionOfficials.Add(adj); } } catch (Api.Client.Exceptions.ApiException ex) { this.errors.Add(ex.InnerException.Message); } }), i)); } Task.Factory.ContinueWhenAll(tasks.ToArray(), new Action <Task[]>(t => { MessageBox.Show("All officials loaded."); })); foreach (var t in tasks) { t.Start(); } }