异步实现方式
async和await,例程如下
using System; using System.Drawing; using System.Windows.Forms; using System.Net; using System.Net.Http; namespace WindowsFormsApplication4 { public { public Form1() { InitializeComponent(); } private { string imageUrl = @”http://www.xudajun.com/wp-content/uploads/2021/09/091621_0200_1.png”; HttpClient client = new HttpResponseMessage response = await client.GetAsync(imageUrl); if(response.StatusCode==HttpStatusCode.OK) { var stream = await response.Content.ReadAsStreamAsync(); Image image = Bitmap.FromStream(stream, true); pictureBox1.Image = image; } } } } |
执行结果
异步实现的示例
private { int d = await GetValueAsync(9); MessageBox.Show(d.ToString()); } private { int c=0; await { Thread.Sleep(10000); c = a * a; }); return c; } |