我想使用线程解除对 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;
}