序言
https://www.cnblogs.com/Vincent-yuan/p/13417356.html
1.同步
private void button1_Click(object sender, EventArgs e) { this.label1.Text = DoWork(); } private string DoWork() { Thread.Sleep(5000); return "StartDo"; }
2.异步(有返回值)
private async void button2_Click(object sender, EventArgs e) { this.label1.Text = await DoWorkAsync(); } private Task<string> DoWorkAsync() { return Task.Run(() => { Thread.Sleep(5000); return "StartDo"; }); }
3.异步(无返回值)
private async void button3_Click(object sender, EventArgs e) { await Task.Run(() => { Thread.Sleep(3000); MessageBox.Show("StartDo"); }); }
4.具有多个await的异步方法
private async void button4_Click(object sender, EventArgs e) { await Task.Run(() =>{ Thread.Sleep(3000);}); MessageBox.Show("1"); await Task.Run(() => { Thread.Sleep(3000); }); MessageBox.Show("2"); await Task.Run(() => { Thread.Sleep(3000); }); MessageBox.Show("3"); }
资料
https://www.cnblogs.com/shanzhiming/p/12439214.html
http://www.cnblogs.com/x-xk/archive/2013/06/05/3118005.html
http://www.cnblogs.com/tdws/p/5679001.html
http://www.cnblogs.com/liqingwen/p/5831951.html
http://www.cnblogs.com/liqingwen/p/5831951.html