【问题标题】:C# Infinite Rainbow Background LoopC# 无限彩虹背景循环
【发布时间】:2017-11-22 14:21:12
【问题描述】:

为什么这个无限的彩虹背景循环不起作用,我在 C# Forms 中运行这段代码,想法是在你点击 button1 后让背景改变颜色。我尝试了不同的无限循环制造商,例如:for(;;)。但这里是代码:

private void button1_Click(object sender, EventArgs e)
        {
    while (true)
    {
         this.BackColor = System.Drawing.Color.Red;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.DarkRed;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.Orange;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.Yellow;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.Green;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.DarkGreen;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.Blue;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.DarkBlue;
         System.Threading.Thread.Sleep(250);
         this.BackColor = System.Drawing.Color.Violet;
    }

谢谢。

【问题讨论】:

  • 您运行此代码的应用程序类型是什么?
  • 这个“不工作”是如何表现出来的?
  • 这段代码在哪里运行?在OnPaint?
  • 因为彩虹不是无限的。它们是由水的光折射引起的。 (换句话说,您需要添加更多信息 - 您正在构建什么样的应用程序等)
  • 你永远不会给 UI 时间来显示任何东西;睡眠也会暂停 UI 线程。每次颜色更改后插入 this.Refresh()! - 顺便说一句,不太可能通过深红色从红色变为橙色。 redorange 会更合理,不是吗?

标签: c# loops infinite


【解决方案1】:

我认为它是 windows 窗体,你不能这样做 Thread.Sleep(n) 因为它是你的窗体,你需要的是一个 Timer,一个快速而肮脏的方式来解决你的问题

public List<Color> colors  = new List<Color> {
    Color.Red,
    Color.DarkRed,
    Color.Orange
};

private int current;
private Timer t = new Timer();
public Form1() {
    InitializeComponent();
    t.Interval = 250;
    t.Tick += T_Tick;

}

private void T_Tick(object sender, System.EventArgs e) {
    this.BackColor = colors[current++]; //change to rainbows other colors
    current %= colors.Count; // rainbow does not have infinite color, we should start again somewhere
}

*your_click_method* {
    t.Start();
}

【讨论】:

  • 谢谢,但是在你点击button1之后彩虹需要启动,比如:private void button1_Click(object sender, EventArgs e) {//这里代码}
【解决方案2】:

除此之外肯定会看起来很可怕, 您的无限循环将阻塞 gui 线程,因此 gui 将永远不会更新。 这意味着您的程序没有时间应用更改的背景颜色。

假设您使用的是 Windows 窗体,您应该在窗体中放置一个时间间隔为 250 毫秒的计时器。 然后将您的颜色存储在一个数组中,列出任何内容并使其成为该表单的成员...

private List<Color> rainbowColors = new List<Color>()
        {
          Color.Red,
          Color.DarkRed,
          ....
        };

您还需要一个索引来了解您当前显示的是哪种颜色。

private int rainbowIndex;

在你的定时器事件上做这样的事情:

private void timer_Elapsed(object sender, EventArgs e)
{
  this.BackColor = this.rainbowColors[this.rainbowIndex++];
  this.rainbowIndex = this.rainbowIndex % this.rainbowColors.Count;

  this.Invalidate(); //Really change the formcolor
}

因此,在每个计时器间隔中,您将进一步选择一种颜色,如果显示最后一种颜色,则将其重置。

【讨论】:

  • 我想你忘记增加索引了。
  • 更简单:BackColor = rainbowColors[(rainbowIndex++) % rainbowColors.Count]
  • 没错,这是一个更清洁的解决方案。
  • @Rotem 是的,它是一行,但是当索引通过时它会给出异常int.Max 将是负数。
  • @tetralobita 您可以将uintunchecked 表达式一起使用,但是是的,您在答案中的做法更简单,更具可读性。
猜你喜欢
  • 1970-01-01
  • 2021-02-27
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2021-07-29
相关资源
最近更新 更多