无法停止计时器

时间:2011-05-14 16:09:38

标签: c# .net timer progress-bar

我无法阻止这个计时器:

    private void startTimer_Click(object sender, EventArgs e)
    {
        AppTimer.Enabled = true;
    }

    private void AppTimer_Tick(object sender, EventArgs e)
    {
        if (BarreProgression.Value < BarreProgression.Maximum)
        {
            ...
            BarreProgression.Value = BarreProgression.Value + 1;
        }
        else if (BarreProgression.Value == BarreProgression.Maximum)
        {

            MessageBox.Show("Finished");
            //AppTimer.Stop();
            AppTimer.Enabled = false;  
        }
    }

我的消息框数量无穷大! 有什么想法吗?

3 个答案:

答案 0 :(得分:4)

MessageBox阻止执行直到用户关闭它,所以先停止计时器然后显示消息,否则你会被windows发送垃圾邮件:

AppTimer.Enabled = false;  
MessageBox.Show("Finished");

答案 1 :(得分:2)

尝试移动定时器停止位置,高一行:

        else if (BarreProgression.Value == BarreProgression.Maximum)
    {

        // This should be above the message box.
        AppTimer.Enabled = false;  
        MessageBox.Show("Finished");
    }

答案 2 :(得分:1)

试试这个,

 if (BarreProgression.Value < BarreProgression.Maximum)
        {
            ...
            BarreProgression.Value = BarreProgression.Value + 1;
        }
        else if (BarreProgression.Value == BarreProgression.Maximum)
        {

            //AppTimer.Stop();
            AppTimer.Enabled = false;  
            MessageBox.Show("Finished");
        }