【问题标题】:How can I slow down my for loop so I can see the colors?我怎样才能减慢我的 for 循环,以便我可以看到颜色?
【发布时间】:2019-10-08 12:09:01
【问题描述】:

我想延迟我的循环,以便它显示我输入的背景颜色。现在,它太快了。

目的是让颜色闪烁以创建迪斯科效果。我使用了Thread.Sleep,但这不起作用。

这是我目前的尝试:

private void Disco(object sender, RoutedEventArgs e)
    {
           for (int n =0; n<=10;n++)
           {
               Background = Brushes.Coral;
               Background = Brushes.AliceBlue;
               Background = Brushes.DarkRed;
               Background = Brushes.Red;
               Background = Brushes.Blue;
               Background = Brushes.Aquamarine;
           }
       }

【问题讨论】:

  • 你不应该使用 for 循环来做这样的动画。您使用的是什么 UI 框架? WPF?
  • 使用 Timer 类可以帮助解决这个问题,它还需要您更改循环流程。
  • 尝试添加Thread.Sleep(5000);低于背景 = .. 行。如果我是你,我会创建一种新的方法来改变背景。
  • 最简单的方法是使用await Task.Delay(500); 并用async 标记您的方法。
  • 仅供参考,如果您在生产代码中使用 Thread.Sleep 用于产生线程以外的任何事情,那么您几乎总是在做一些非常错误的事情。线程是用来工作的,而不是睡觉的。

标签: c# xaml


【解决方案1】:

您可以使用 @Jeroen van Langen 在 cmets 中建议的 async 方法。

Background 更改之间使用await Task.Delay(time) 将延迟给定time 的更改。

private async void Disco(object sender, RoutedEventArgs e)
{
    Background = Brushes.Coral;
    await Task.Delay(500); // 500 is just an example. You can use any number in milliseconds.
    Background = Brushes.AliceBlue;
    await Task.Delay(500);
    Background = Brushes.DarkRed;
    await Task.Delay(500);
    Background = Brushes.Red;
    await Task.Delay(500);
    Background = Brushes.Blue;
    await Task.Delay(500);
    Background = Brushes.Aquamarine;
    await Task.Delay(500);
    //Set the background back to its original color here.
}

如果您需要您的表单继续“迪斯科”,直到再次按下按钮,您可以将它包装在一个 while 循环中,该循环将一直持续到按下 Button 或 @ 987654329@被选中。

如果你走这条路,我建议你阅读asynchronous programming documentation。这非常有帮助。

【讨论】:

    【解决方案2】:

    此代码适用于 mi,每 5 秒更改一次背景颜色:

    公共部分类Form1:表格 { /// /// 控制顺序的颜色列表 /// Color[] colorsList = { Color.Coral, Color.AliceBlue, Color.DarkRed, Color.Red, Color.Blue, Color.Aquamarine }; /// <summary> /// Timer to control the execution time of the color change /// </summary> System.Timers.Timer myTimer = new System.Timers.Timer(); public Form1() { InitializeComponent(); } /// <summary> /// Load or Main of class /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { this.BackColor = colorsList[0]; myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); myTimer.Interval = 5000; myTimer.Start(); } /// <summary> /// method that changes the color, executed by the timer /// </summary> /// <param name="source"></param> /// <param name="e"></param> private void OnTimedEvent(object source, ElapsedEventArgs e) { for (int i = 0; i < colorsList.Length; i++) { if (this.BackColor == colorsList[i]) { if (i < colorsList.Length - 1) { this.BackColor = colorsList[i + 1]; break; } else { //return to the first color this.BackColor = colorsList[0]; break; } } } } }

    【讨论】:

    • 这将始终将背景设置为海蓝宝石。
    • 你必须为每种颜色创建一个方法
    • 假设计时器都大致同步,那么您将遇到同样的问题(或更糟)。这不是解决方案,我认为您可能需要重新考虑该方法。
    • 这根本不是解决这个问题的正确方法。如果您要使用计时器,请使用 one 计时器,而不是每种颜色的计时器!当然不要为每种颜色制作一种方法。
    • 示例代码快速变成生产代码;示例代码应该是最佳实践代码。向初学者展示最佳实践!
    猜你喜欢
    • 2014-05-14
    • 2023-02-02
    • 1970-01-01
    • 2013-08-18
    • 2015-07-04
    • 2014-06-18
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    相关资源
    最近更新 更多