我怎么能停止我的while循环?

时间:2009-05-01 17:46:24

标签: c#

我想在C#中使用以下代码,但我似乎无法摆脱它。如果用户按下某个键或移动啮齿动物(又称鼠标),我想终止该应用程序。这是我的代码(不笑!)。

private void frmDots_KeyDown(object sender, KeyEventArgs e)
{
  bgNotClicked = false;
  Close();
}   

private void frmDots_Click(object sender, EventArgs e)
{
  bgNotClicked = false;
  Close();
}   



  while (bgNotClicked)
  {

    // Clear the first element in our XY position. This is the reverse of the way I   normally create the dots application
    System.Drawing.Rectangle clearDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);

    // Create the black color and brush to clear dots
    Color clearDotsColor = Color.Black;
    SolidBrush clearDotsBrush = new SolidBrush(clearDotsColor);

    // Finally clear the dot
    e.Graphics.FillEllipse(clearDotsBrush, clearDots);


    GetRandomPosition(iCounter);

    // Fill the elements to display colors on the displays canvas
    System.Drawing.Rectangle colorDots = new System.Drawing.Rectangle(Dots.PositionX[iCounter], Dots.PositionY[iCounter], 8, 8);

    // Create the color and brush to show dots
    Color colorRandom = GetRandomColor();
    SolidBrush colorBrush = new SolidBrush(colorRandom);

    // Finally show the dot
    e.Graphics.FillEllipse(colorBrush, colorDots);

    Thread.Sleep(5);

    iCounter++;
    if (iCounter == 399)
    {
      iCounter = 0;
    }

  }
}

11 个答案:

答案 0 :(得分:6)

你的“忙碌等待”策略是糟糕的设计。相反,您应该使用被触发的事件处理程序:

  • On keypress。
  • 移动鼠标时。

在任何一种情况下,您都可以通过终止申请来做出回应。

答案 1 :(得分:4)

编辑:

看到你的编辑后,这绝对是你的问题。问题是你的while循环阻塞了主UI线程,因此它永远不会处理触发按键/鼠标/等处理程序的Windows消息。

您有几个选项 - 您可以将其中的一部分移到单独的线程中,执行下面建议的操作,或者在while循环中添加对Application.DoEvents的调用。这将允许您的事件处理程序运行,然后设置bgNotClicked = false;。现在,这种情况从未发生过,因为您的UI完全被阻止了。


原帖:

如果您在UI线程中执行此循环,则需要重新考虑设计。

在事件处理程序中的某处设置bgNotClicked = false;将起作用,但前提是您的事件处理程序能够运行。如果您在UI线程中执行上述代码,它将无限期地阻止您的UI线程,从而阻止事件处理程序触发。

我建议将其重新设置为基于计时器(因此它会定期重复运行),而不是锁定到while循环中。这将允许您的其他UI事件在运行之间触发,而不是设置bgNotClicked = false;,您的事件处理程序可能只是将计时器设置为未启用。

答案 2 :(得分:3)

您的 bgNotClicked 变量需要由您的事件处理程序设置为 false 以进行按键操作。

如果用鼠标移动啮齿动物,则需要一个类似的鼠标事件处理程序。

答案 3 :(得分:1)

break关键字将终止循环。在这种情况下,当您遇到要停止循环的情况时,只需使用break;

答案 4 :(得分:1)

如果你像这样循环,你需要给应用程序一个时间来处理你希望会导致中断的事件。这是DoEvents方法的工作。

private bool WeAreDone = false;

private void DoingIt()
{
    while (true)
    {
        Application.DoEvents();
        if (WeAreDone)
        {
            break;
        }
    }
}

private void InterruptButton_Click(object sender, EventArgs e)
{
    WeAreDone = true;
}

答案 5 :(得分:1)

我认为使用Timer比忙碌等待循环更适合Windows事件驱动模型。您可以尝试这样的事情:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private int iCounter = 0;

    private void Draw()
    {
        // ....
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Draw();

        iCounter++;
        if(iCounter == 399)
        {
            iCounter = 0;
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Interval = 5;
        timer1.Enabled = true;
    }

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

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        timer1.Enabled = false;
        Close();
    }
}

答案 6 :(得分:0)

您可以使用中断语句退出循环。

编辑:好的,我拿回旗帜的东西!

答案 7 :(得分:0)

这似乎不是正确的方法。 .Net Framework为您提供了处理KeyPress和MouseMove / Click操作的事件。你为什么不使用它们?

答案 8 :(得分:0)

尝试将循环移动到BackgroundWorker的DoWork事件处理程序中。然后你的GUI仍然会响应,而不是那个讨厌的变量,你可以调用CancelAsync方法来停止循环。

答案 9 :(得分:0)

使用Environment.Exit(2);(或Environment.Exit(1)它确实没有任何区别)退出应用程序。

答案 10 :(得分:-3)

Exit While

...................

相关问题