是否有一種簡單的方法可以讓表單上的元素在我單擊 Windows Show Desktop 后繼續更新?以下代碼更新 textBox1 中的值,直到我單擊 Windows Show Desktop(Windows 10 - 單擊螢屏右下角)。我不喜歡使用 Application.DoEvents()
private async void Button1Click(object sender, EventArgs e)
{
int n = 0;
while (true) {
Task<int> task = Increment(n);
var result = await task;
n = task.Result;
textBox1.Text = n.ToString();
textBox1.Refresh();
Update();
// await Task.Delay(200);
}
}
public async Task<int> Increment(int num)
{
return num;
}
uj5u.com熱心網友回復:
解決此問題ThreadPool的Task.Run一種方法是使用以下方法將受 CPU 限制的作業卸載到執行緒:
private async void Button1Click(object sender, EventArgs e)
{
int n = 0;
while (true)
{
n = await Task.Run(() => Increment(n));
textBox1.Text = n.ToString();
}
}
此解決方案假定該Increment方法不會以任何方式在內部與 UI 組件互動。如果您確實需要與 UI 互動,則上述方法不是一種選擇。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394757.html
