Home > Enterprise >  Windows Show Desktop causes WinForms elements to stop updating
Windows Show Desktop causes WinForms elements to stop updating

Time:12-26

Is there a simple way to have elements on a form keep updating even after I click on Windows Show Desktop? The following code updates the value in textBox1 until I click on Windows Show Desktop (Windows 10 - click on the bottom right of the screen). I prefer not to use 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);
        }
    }

CodePudding user response:

Using Thread.Sleep blocks the current thread (UI thread); you can fix it like this:

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