我希望您能够使用线程解除UI阻塞。
您能告诉我以下代码之间的区别吗:
您能告诉我以下代码之间的区别吗:
private async void button1_Click(object sender, EventArgs e)
{
int result = await Calculate(1, 2);
label1.Text = result.ToString();
}
private async Task<int> Calculate(int number1, int number2)
{
return await Task.Run(() =>
{
Thread.Sleep(5000);
return number1 + number2;
});
}
而这段代码:
private async void button1_Click(object sender, EventArgs e)
{
int result = await Calculate(1, 2);
label1.Text = result.ToString();
}
private async Task<int> Calculate(int number1, int number2)
{
await Task.Delay(5000);
return number1 + number2;
}