C#倒计时定时器在循环中击中墙? [英] C# Countdown Timer Hits A Wall In The Loop?

查看:74
本文介绍了C#倒计时定时器在循环中击中墙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家都在CodeProject上了!





所以我正在为我的朋友做一个小项目&学习目的。

它基本上是一个倒数计时器&这个想法开始很简单,

创建2个计时器&添加一些标签,以显示分钟和时间。还剩几秒。

我把它重置为0秒回到60,所以秒部分工作正常,但这是问题。



当我按下开始按钮时,它启动计时器,但只有一秒钟,然后我将不得不再按开始。

&我尝试了一些if循环,但它只是不想工作。

For循环会修复这个问题吗?



你有什么想法。



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;

< pre lang = c#>命名空间DeadManModeTimer
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void Start_Click( object sender,EventArgs e)
{
// 执行计时器的开始
timer1.Start();

// 启用计时器见第36行& 49
timer1.Enabled = true ;
timer2.Enabled = true ;
}

// 分钟/秒的持有人
int m = 30 ;
int s = 60 ;

private void timer1_Tick(对象发​​件人,EventArgs e)
{
timer1.Enabled = false ;
s--;
label1.Text = s.ToString()+ ;

if (s < = 0
{
s = 60 ;
}
if (timer1.Enabled == true
{
s--;
}

}

private void timer2_Tick( object sender,EventArgs e)
{
timer2.Enabled = false ;

m--;
label2.Text = m.ToString()+ Minutes;

if (m < = 0
{
m = 30 ;
}
}
}
}

解决方案

你不需要任何循环。

只需删除代码行

 timer1.Enabled = false; 



 timer2.Enabled = false; 



来自你的代码,定时器不会被禁用。


对于初学者来说,如果不是一个循环 - 这是一个有条件的,它只执行一次而不能循环回来。



其次,你根本不启用timer2!



第三,这是一些奇怪的代码,因为你禁用了定时器当它得到它的嘀嗒事件时你做的第一件事 - 这意味着除非你再次启用它,否则它不会给出第二个刻度,你没有。



废话,重新开始。

而不是两次,有一次,而不是保持分钟和秒数,而不是使用目标DateTime值:

private DateTime countdownTarget;
...
countdownTarget = DateTime.Now.AddSeconds( 60 +( 30 * 60 ));
myTimer.Start();



然后,在您的Tick处理程序中检查目标:

 TimeSpan diff = countdownTarget  -  DateTime.Now; 
int secs = diff.Seconds;
int mins = diff.Minutes;
if (diff.TotalSeconds < = 0
{
countdownTarget = DateTime.Now.AddSeconds( 60 +( 30 * 60 ));
}

然后,您可以使用这两个值来设置标签。


Hey everyone here on CodeProject!


So I am working on a small project for my friends & learning purposes.
It's basically a countdown timer & the idea started out simple,
Create 2 timers & add some labels to it for it to show the minutes & seconds left.
I got it to reset at 0 seconds back to 60 so the seconds part is working fine but here is the issue.

When I press the start button it starts the timer but only for a second then I will have to press start again.
& I've tried some if loops but it just didnt want to work.
Would a For loop fix the issue?

What are your thoughts.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

<pre lang="c#">namespace DeadManModeTimer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            //Executes the start of the timers
            timer1.Start();

            //Enables the timers see line 36 & 49
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        //Holders For Minutes / Seconds
        int m = 30;
        int s = 60;

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            s--;
            label1.Text = s.ToString() + "Seconds ";

            if (s <= 0)
            {
                s = 60;
            }
            if (timer1.Enabled == true)
                {
                s--;
                }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Enabled = false;

            m--;
            label2.Text = m.ToString() + "Minutes ";

            if (m <= 0)
            {
                m = 30;
            }
        }
    }
}

解决方案

You don't need any loops.
Just remove the lines of code

timer1.Enabled = false;


and

timer2.Enabled = false;


from your code and the timers will not be disabled.


For starters, if isn't a loop - it's a conditional, it's only executed once and can't "loop back" on itself.

Second, you don't enable timer2 at all!

Third, that's some odd code, given that you disable the timer as the first thing you do when it gets it's tick event - which means it won't give a second tick unless your enable it again, which you don't.

Scrap that, are start again.
Instead of having two times, have one, and instead of keeping a minutes and seconds count, use a "target" DateTime value:

private DateTime countdownTarget;
...
   countdownTarget = DateTime.Now.AddSeconds(60 + (30 * 60));
   myTimer.Start();


Then, in your Tick handler check the target:

TimeSpan diff = countdownTarget - DateTime.Now;
int secs = diff.Seconds;
int mins = diff.Minutes;
if (diff.TotalSeconds <= 0)
   {
   countdownTarget = DateTime.Now.AddSeconds(60 + (30 * 60));
   }

You can then use the the two values to set your labels.


这篇关于C#倒计时定时器在循环中击中墙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆