gpt4 book ai didi

C#:如何在滴答中间停止计时器

转载 作者:行者123 更新时间:2023-11-30 20:25:45 25 4
gpt4 key购买 nike

我在 form1(windows 窗体)中设置了一个定时器,每秒滴答一次

private void Form1_Load(object sender, EventArgs e)
{
Timer.Interval = 1000;
Timer.Start;
}

在 timer.tick 事件中,我将其设置为在一定数量的滴答后停止。

 private void Timer_Tick(object sender, EventArgs e)
{
if(x == 0)
{
MessageBox.Show("ETC");
Timer.Stop();
}
}

但是,我发现 timer.Stop() 并没有结束计时器,即使在 x = 0 之后,消息框仍然每秒弹出一次。这是为什么?我该如何停止计时器?

这是完整的代码

 private void btnStart_Click(object sender, EventArgs e)
{
checkTiming.Stop();

try
{
timeTick.hour = int.Parse(textBoxHour.Text);
timeTick.min = int.Parse(textBoxMin.Text);
timeTick.sec = int.Parse(textBoxSec.Text);
numRepeat = int.Parse(textBoxRepeat.Text);
timeTick.totalTime = 0;
current = timeTick.hour * 3600 + timeTick.min * 60 + timeTick.sec;

if (timeTick.hour * 3600 + timeTick.min * 60 + timeTick.sec > 0 && timeTick.min <= 60 && timeTick.sec <=60 )
{

memory.listHistory.Add(memory.padMultipleString(textBoxHour.Text, textBoxMin.Text, textBoxSec.Text));
updateListViewHistory(memory.listHistory);

checkTiming.Interval = 1000;
checkTiming.Start();
}

else
{
MessageBox.Show("Please enter a valid value", "Error", MessageBoxButtons.OK);
initialise();
}
}

catch
{
MessageBox.Show("Please enter positive integers to all textBoxes", "Error", MessageBoxButtons.OK);
initialise();
}
}

这里是滴答事件

 private void checkTiming_Tick(object sender, EventArgs e)
{

timeTick.updateTime(current);
updateTextBoxes();

if(current > 0)
{
current--;
}

if(current == 0)
{
if(numRepeat > 1)
{
numRepeat--;
current = timeTick.totalTime;
//Console.WriteLine(current);

MessageBox.Show(memory.listHistory.Last() + " has elapsed. " + "Repeating " + numRepeat.ToString() + " more times", "Timing has Ended", MessageBoxButtons.OK);
}

if(numRepeat == 1)
{
MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
timeTick.totalTime = 0;
Console.WriteLine(numRepeat);
checkTiming.Stop();
}
}



}

主要问题出在这部分

       private void checkTiming_Tick(object sender, EventArgs e)
{

timeTick.updateTime(current);
updateTextBoxes();

if(current > 0)
{
current--;
}

if(current == 0)
{
if(numRepeat > 1)
{
numRepeat--;
current = timeTick.totalTime;
//Console.WriteLine(current);

MessageBox.Show(memory.listHistory.Last() + " has elapsed. " + "Repeating " + numRepeat.ToString() + " more times", "Timing has Ended", MessageBoxButtons.OK);
}

if(numRepeat == 1)
{
MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
timeTick.totalTime = 0;
Console.WriteLine(numRepeat);
checkTiming.Stop();
}
}

即使当 numRepeat 为 1 且 current = 0 时,即使我声明它(checkTiming.Stop()),计时器也不会停止

最佳答案

根据documentation , 消息框:

Displays a message window, also known as a dialog box, which presents a message to the user. It is a modal window, blocking other actions in the application until the user closes it.

这意味着当控件到达 MessageBox.Show 行时,它将停在那里,直到用户关闭消息框。这意味着在用户关闭消息框之前不会调用 Timer.Stop。这就是计时器仍会滴答作响的原因。

要解决这个问题,只需更改方法调用的顺序:

   checkTiming.Stop();
MessageBox.Show(memory.listHistory.Last() + " has elapsed", "Timing has Ended", MessageBoxButtons.OK);
timeTick.totalTime = 0;
Console.WriteLine(numRepeat);

关于C#:如何在滴答中间停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50998252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com