5
我想用線程解鎖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;
}
可能重複[Thread.Sleep vs Task.Delay?](http://stackoverflow.com/questions/17258428/thread-sleep-vs-task-delay) – Muctadir