Explain, please, why in both for loops in the example is written && Visible , if Visible already in While ? When compiling the program I can not see the difference that with && Visible in for loops, that without.
private void button1_Click(object sender, EventArgs e) { while (Visible) { for (int c = 0; c < 255 && Visible; c++) { this.BackColor = Color.FromArgb(c, 255 - c, c); Application.DoEvents(); System.Threading.Thread.Sleep(5); } for (int c = 254; c>=0 && Visible; c--) { this.BackColor = Color.FromArgb(c, 255-c, c); Application.DoEvents(); System.Threading.Thread.Sleep(5); } } }
Visibleform property is alwaystrue. It is not needed here at all. Ah,DoEvents! This means that during this cycle an event can occur that causes the execution of another code that can change the value ofVisibletofalse. - IgorDoEventsever. Do not use nastyThread.Sleepnever. Use the divineasync/await. - VladDSynchronizationContext! Try it yourself: pastebin.com/k3HJzfV5 - VladD