【问题标题】:Proper way to add delay in my code execution在我的代码执行中添加延迟的正确方法
【发布时间】:2014-12-16 23:08:09
【问题描述】:

我正在尝试制作一个非常简单的逻辑游戏。这个想法是看到一个带有一定数量彩色方块(按钮)的矩阵,然后隐藏它们,玩家必须点击彩色方块。所以我需要在绘制正方形/按钮和返回原始颜色之间有 2 秒的延迟。所有代码都在button_click 事件中实现。

private void button10_Click(object sender, EventArgs e)
{

    int[,] tempMatrix = new int[3, 3];
    tempMatrix = MakeMatrix();
    tempMatrix = SetDifferentValues(tempMatrix);
    SetButtonColor(tempMatrix, 8);
    if (true)
    {
        Thread.Sleep(1000);
       // ReturnButtonsDefaultColor();
    }
    ReturnButtonsDefaultColor();
    Thread.Sleep(2000);

    tempMatrix = ResetTempMatrix(tempMatrix);
}

这是整个代码,但我需要在调用SetButtonColor()ReturnButtonsDefaultColor() 之间有一些延迟。到目前为止,我对Thread.Sleep() 的所有实验都没有成功。我在某些时候遇到了延迟,但从未显示彩色方块/按钮。

【问题讨论】:

  • 在代码完成之前不会绘制 UI。

标签: c# winforms


【解决方案1】:

您看不到按钮颜色的变化,因为Sleep 调用阻止了消息被处理。

处理此问题的最简单方法可能是使用计时器。以 2 秒的延迟初始化计时器,并确保它默认禁用。然后,您的按钮单击代码启用计时器。像这样:

private void button10_Click(object sender, EventArgs e)
{
    // do stuff here
    SetButtonColor(...);
    timer1.Enabled = true; // enables the timer. The Elapsed event will occur in 2 seconds
}

还有你的计时器的 Elapsed 事件处理程序:

private void timer1_TIck(object sender, EventArgs e)
{
    timer1.Enabled = false;
    ResetButtonsDefaultColor();
}

【讨论】:

    【解决方案2】:

    您始终可以使用 TPL,它是最简单的解决方案,因为您不需要处理线程上下文或分段代码。

    private async void button10_Click(object sender, EventArgs e)
    {
    
        int[,] tempMatrix = new int[3, 3];
        tempMatrix = MakeMatrix();
        tempMatrix = SetDifferentValues(tempMatrix);
        SetButtonColor(tempMatrix, 8);
        if (true)
        {
           await Task.Delay(2000);
           // ReturnButtonsDefaultColor();
        }
        ReturnButtonsDefaultColor();
           await Task.Delay(2000);
    
        tempMatrix = ResetTempMatrix(tempMatrix);
    }
    

    【讨论】:

    • 不,一旦代码到达await,函数就会返回,UI线程被释放。延迟结束后,UI 线程将继续执行
    【解决方案3】:

    使用Timer。而不是你的 Thread.Sleep Start() 计时器,在滴答事件调用 ReturnButtonsDefaultColor()。您可以使用第二个计时器而不是第二个 Thread.Sleep 或保存某种状态并在滴答事件中使用它。

    【讨论】:

      【解决方案4】:

      您可以使用任务:

              private void button10_Click(object sender, EventArgs e)
              {
      
                  int[,] tempMatrix = new int[3, 3];
                  tempMatrix = MakeMatrix();
                  tempMatrix = SetDifferentValues(tempMatrix);
                  SetButtonColor(tempMatrix, 8);
      
                  Task.Factory.StartNew(
                      () => 
                      {
                          if (true)
                          {
                              Thread.Sleep(1000);
                              // ReturnButtonsDefaultColor();
                          }
                          ReturnButtonsDefaultColor(); //Need to dispatch that to the UI thread
                          Thread.Sleep(2000);
      
                          tempMatrix = ResetTempMatrix(tempMatrix); //Probably that as well
                      });
              }
      

      WPF 中的调度与 Winforms 不同,google 应该很容易 ;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-02
        • 2016-12-11
        • 1970-01-01
        相关资源
        最近更新 更多