private async void button2_Click(object sender, EventArgs e) { button1.Enabled = false; button2.Enabled = false; var n = Convert.ToInt32(textBox2.Text); string txt = "top 1 轮循" + n + "次\r\n"; txtResult.Clear(); txtResult.AppendText(txt); await Task.Run(() => { long useTime; foreach (var kv in methods) { var item = kv.Value; var counter = new CounterWatch(); counter.Start(kv.Key, () => { item(1); }, n); txt = counter.ToString() + "\r\n"; if (count2.ContainsKey(kv.Key)) { count2[kv.Key] += counter.ElapsedMilliseconds; } else { count2.Add(kv.Key, counter.ElapsedMilliseconds); } this.BeginInvoke(new Action(() => { txtResult.AppendText(txt); })); } this.BeginInvoke(new Action(() => { button1.Enabled = true; button2.Enabled = true; showTotal(dataGridView2, count2); })); }); }
private async void button1_Click(object sender, EventArgs e) { button1.Enabled = false; button2.Enabled = false; var n = Convert.ToInt32(textBox1.Text); string txt = "查询 top " + n + "行数据\r\n"; txtResult.Clear(); txtResult.AppendText(txt); long useTime; await Task.Run(() => { foreach (var kv in methods) { System.Threading.Thread.Sleep(300); var method = kv.Value; var counter = new CounterWatch(); counter.Start(kv.Key, () => { method(n); }, 3); txt = counter.ToString() + "\r\n"; if (count1.ContainsKey(kv.Key)) { count1[kv.Key] += counter.ElapsedMilliseconds; } else { count1.Add(kv.Key, counter.ElapsedMilliseconds); } this.BeginInvoke(new Action(() => { txtResult.AppendText(txt); })); } this.BeginInvoke(new Action(() => { button1.Enabled = true; button2.Enabled = true; showTotal(dataGridView1, count1); })); }); }