简体   繁体   English

我如何才能在实时C#中以秒为单位停止计时器?

[英]How I can stop a timer with seconds in real time c#?

I'm trying to stop a timer when 16 seconds in real time have passed, but i don't know how i can do that. 我正在尝试在经过实时16秒后停止计时器,但是我不知道该怎么做。

I made this little example: when picturebox1 intersects with picturebox2,this action activate a timer, and this timer have to shows the picturebox3 during 16 seconds in real time and after stop it(timer) (and the picturebox3 doesn't show). 我举了一个小例子:当picturebox1与picturebox2相交时,此动作将激活一个计时器,并且该计时器必须在16秒钟内实时显示picturebox3,并在将其停止(计时器)后显示(而picturebox3不显示)。

(Sorry for my english. But StackOverflow in Spanish doesn't have many information). (对不起,我的英语。但是西班牙语的StackOverflow信息不多)。

I'm using windows form and C# 我正在使用Windows窗体和C#

    private void timer2_Tick(object sender, EventArgs e)
    {
        pictureBox7.Hide();
        if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible) || (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
        {                
            puntaje++;
            this.Text = "Puntaje: " + puntaje;
            if (puntaje % 5 == 0)
            {
               timer3.Enabled=true;
 //This is the part where i want set down the timer3, timer 2 is on

            }
        }

You can try this, on your timer tick event handler. 您可以在计时器滴答事件处理程序上尝试执行此操作。 Timespan counts the elapsed time between two dates. 时间跨度计算两个日期之间经过的时间。 On this case since its 16 seconds, we count it by negative. 在这种情况下,自16秒钟以来,我们将其计为负数。

private void timer1_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = dtStart.Subtract(DateTime.Now);
        if (ts.TotalSeconds <= -16)
        {
            timer1.Stop();
        }
    }

Make sure your dtStart (DateTime) is declared when you start your timer: 确保在启动计时器时声明了dtStart(DateTime):

timer1.Start();
dtStart = DateTime.Now;

The cleanest way I can see this implemented is by using the interval parameter of a System.Timers.Timer . 我可以看到的最干净的方法是使用System.Timers.Timer的interval参数。

Here's a sample snippet of the code 这是代码示例

var timer = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
timer.Elapsed += (sender, e) =>
{
    Console.WriteLine($"Finished at exactly {timer.Interval} milliseconds");
};
_timer.Start();

The TimeSpan.FromSeconds(16).TotalMilliseconds basically converts to 16000 but I used the TimeSpan static method for you to understand it easier and looks more readable. TimeSpan.FromSeconds(16).TotalMilliseconds基本上可以转换为16000,但是我使用TimeSpan静态方法让您更容易理解它,并使其更具可读性。

The AutoReset property of the timer tells it that it should only be triggered once. 计时器的AutoReset属性告诉它仅应触发一次。

Adjusted for your code 根据您的代码进行了调整

private void timer2_Tick(object sender, EventArgs e)
{
    pictureBox7.Hide();
    if ((pictureBox3.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible) 
        || (pictureBox5.Bounds.IntersectsWith(pictureBox2.Bounds) && pictureBox2.Visible))
    {                
        puntaje++;
        this.Text = "Puntaje: " + puntaje;
        if (puntaje % 5 == 0)
        {
            var timer3 = new Timer(TimeSpan.FromSeconds(16).TotalMilliseconds) { AutoReset = false };
            timer3.Elapsed += (sender, e) =>
            {
                pictureBox3.Visible = true;
            };
            timer3.Start();
        }
    }
}

Please do mark the question Answered if this solves your issue. 如果可以解决您的问题,请标记为已回答问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM