Windows 显示桌面导致 WinForms 元素停止更新

Windows Show Desktop causes WinForms elements to stop updating

有没有一种简单的方法可以让表单上的元素在我单击 Windows 显示桌面后继续更新?以下代码更新 textBox1 中的值,直到我单击 Windows 显示桌面(Windows 10 - 单击屏幕右下方)。我不想使用 Application.DoEvents().

void Button1Click(object sender, EventArgs e)
{
    int n = 0;
    while(true) {
        textBox1.Text = n++.ToString();
        textBox1.Refresh();
        Update();
        // Application.DoEvents();
        Thread.Sleep(200);
    }
}

使用Thread.Sleep阻塞当前线程(UI线程);你可以这样修复它:

private async void button1_Click(object sender, EventArgs e)
{
    int n = 0;
    while (true)
    {
        textBox1.Text = n++.ToString();
        await Task.Delay(200);
    }
}