private void btnRead_Click(object sender, EventArgs e) { if (cbPrice.SelectedIndex < 0) { return; } btnRead.Enabled = false; pBar.Visible = true; cbPrice.Enabled = false; using (SVPEntitiesContainer context = new SVPEntitiesContainer()) { Profile profile = ((Price)cbPrice.SelectedItem).Profile; System.Threading.Tasks.Task <List <RMResult> > ta = System.Threading.Tasks.Task.Factory.StartNew <List <RMResult> >(() => Common.readShots(profile.Value)); while (!ta.IsCompleted) { Application.DoEvents(); } if (ta.Result == null) { pBar.Visible = false; cbPrice.Enabled = true; btnRead.Enabled = true; return; } Sequence sequence = new Sequence(); sequence.Date = DateTime.Now; sequence.Member = ((Member)cbMember.SelectedItem); sequence.Profile = profile; sequence.Price = ((Price)cbPrice.SelectedItem); context.Participants.Attach(sequence.Member); context.Profiles.Attach(sequence.Profile); context.Prices.Attach(sequence.Price); cbPrice.Items.Remove(cbPrice.SelectedItem); foreach (RMResult result in ta.Result) { Shot s = new Shot(); s.Value = result.Rings; s.Angle = result.Angle; s.FactorValue = result.FactorValue; s.ShotNumber = (short)result.ShotNumber; s.Valid = (result.Validity == ValidFlag.Valid); sequence.Shots.Add(s); } DataGridViewRow row = new DataGridViewRow(); row.Tag = sequence.Id; row.Cells.Add(new DataGridViewTextBoxCell() { Value = sequence.Member.ToString() }); row.Cells.Add(new DataGridViewTextBoxCell() { Value = sequence.Shots.Sum(s => s.Value) }); row.Cells.Add(new DataGridViewTextBoxCell() { Value = sequence.Profile.ToString() }); row.Cells.Add(new DataGridViewTextBoxCell() { Value = sequence.Price.ToString() }); row.Cells.Add(new DataGridViewButtonCell() { UseColumnTextForButtonValue = true, Value = sequence }); dvResults.Rows.Add(row); context.Sequences.Add(sequence); context.SaveChanges(); if (cbPrice.Items.Count > 0) { cbPrice.SelectedIndex = 0; } Monitor.GetMonitor().AddSequence(context.Sequences.Include("Shots").First(x => x.Id == sequence.Id)); gbRead.Enabled = (cbPrice.Items.Count > 0); btnRead.Enabled = true; pBar.Visible = false; cbPrice.Enabled = true; } }
private void btnRead_Click(object sender, EventArgs e) { if (cbProfile.SelectedIndex < 0) { return; } Sequence sequence; btnRead.Enabled = false; pBar.Visible = true; using (SVPEntitiesContainer context = new SVPEntitiesContainer()) { Profile profile = context.Profiles.Where(x => x.Id == ((ComboboxItem)cbProfile.SelectedItem).Id).First(); Member member = context.Participants.OfType <Member>().Where(x => x.Id == ((Member)cbMember.SelectedItem).Id).First(); Task <List <RMResult> > ta = Task.Factory.StartNew <List <RMResult> >(() => readShots(profile.Value)); while (!ta.IsCompleted) { Application.DoEvents(); } if (ta.Result == null) { pBar.Visible = false; btnRead.Enabled = true; return; } sequence = new Sequence(); sequence.Date = DateTime.Now; sequence.Member = member; sequence.Profile = profile; context.Participants.Attach(sequence.Member); foreach (RMResult result in ta.Result) { Shot s = new Shot(); s.Value = result.Rings; s.Angle = result.Angle; s.FactorValue = result.FactorValue; s.ShotNumber = (short)result.ShotNumber; s.Valid = (result.Validity == ValidFlag.Valid); sequence.Shots.Add(s); } context.Sequences.Add(sequence); context.SaveChanges(); sequence = context.Sequences.Where(x => x.Id == sequence.Id).FirstOrDefault(); DataGridViewRow row = new DataGridViewRow(); row.Tag = sequence.Id; row.Cells.Add(new DataGridViewTextBoxCell() { Value = member.ToString() }); row.Cells.Add(new DataGridViewTextBoxCell() { Value = sequence.Shots.Sum(s => s.Value) }); row.Cells.Add(new DataGridViewTextBoxCell() { Value = profile.ToString() }); row.Cells.Add(new DataGridViewButtonCell() { UseColumnTextForButtonValue = true, Tag = sequence.Id }); dvResults.Rows.Add(row); } pBar.Visible = false; Monitor.GetMonitor().AddSequence(sequence); }