日期时间计时器时间间隔不会改变加
DateTime newDate = new DateTime(2013, 1, 1);
void AddTime()
{
timer1.Interval = 600000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
newDate = newDate.AddMonths(+3);
lblDate.Text = newDate.ToString();
}
出于某种原因改变timer1.Interval
不会改变3个月的速度被添加到newDate
个月的速度,它始终是不变的。我想在比赛中有1分钟的现实生活时间等于3个月。 我正在使用C#。日期时间计时器时间间隔不会改变加
确保值.Interval是您想要的值。 你有600 000秒,即600秒或10分钟。 你有没有足够的时间来运行该事件? 调试它并放置一个breakpoing。
你的间隔是太高目前,它的6000秒而不是60:
DateTime newDate = new DateTime(2013, 1, 1);
void AddTime()
{
timer1.Interval = 60000; // was 600 seconds, now 60
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
newDate = newDate.AddMonths(3); // + sign shouldn't be here
lblDate.Text = newDate.ToString();
}
编辑: 现在我明白了,你是不是叫此刻添加时间(),和目前还不清楚在哪里做。没有更多信息很难说,但如果您使用Winforms,则可以使用表单的加载事件。或者如果它是一个类,你可以使用构造函数来调用它。
基本上是初始化您正在使用的对象的方法。
当我运行这个代码时,它仍然每秒增加3个月(不确定的确切时间),但它的非常短和一致 – algoBaller 2013-05-14 17:28:39
@NikitaGusev我们需要看到更多的代码来帮助你。 – pyrocumulus 2013-05-14 17:49:06
您的初始计时器间隔较大。以下是示例完整应用程序。按预期方式工作
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DateTime newDate = new DateTime(2013, 1, 1);
public Form1()
{
InitializeComponent();
AddTime(); // call the method, otherwise timer will not start
}
void AddTime()
{
timer1.Interval = 60000; // every minute (1 minute = 60000 milliseconds)
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
newDate = newDate.AddMonths(3);
label1.Text = newDate.ToString();
}
// if you need to set timet interval after timer start, do as below
private void button1_Click(object sender, EventArgs e)
{
timer1.Stop();
timer1.Interval = 30000; // set interval 30 seconds
timer1.Start();
}
}
}
你正在以错误的方式前进。首先计算“游戏时间”到“正常时间”的RATIO。然而,月份是有问题的,因为一个月中的天数是可变的。相反,我们可以使用四分之一(365/4)并从那里开始工作。使用秒表追踪已经过了多少时间,并将其添加到参考日期以获得“实时”。然后,“比赛时间”就是经过时间乘以比率,然后加到参考时间。使用此模型时,Timer Interval()为REREVELANT;我们可以更新一分钟一次,每秒一次,或第二四次,以确定真正的/游戏时间的代码是完全一样的... ...并始终保持正确的,当我们更新显示:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// update once per second, but the rate here is IRREVELANT...
// ...and can be changed without affecting the real/game timing
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
}
private DateTime dtReal;
private DateTime dtGame;
private DateTime dtReference;
private System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch();
private double TimeRatio = (TimeSpan.FromDays(365).TotalMilliseconds/4.0)/TimeSpan.FromMinutes(1).TotalMilliseconds;
private void button1_Click(object sender, EventArgs e)
{
StartTime();
}
private void StartTime()
{
dtReference = new DateTime(2013, 1, 1);
SW.Restart();
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
UpdateTimes();
DisplayTimes();
}
private void UpdateTimes()
{
double elapsed = (double)SW.ElapsedMilliseconds;
dtReal = dtReference.AddMilliseconds(elapsed);
dtGame = dtReference.AddMilliseconds(elapsed * TimeRatio);
}
private void DisplayTimes()
{
lblReference.Text = dtReference.ToString();
lblReal.Text = dtReal.ToString();
lblGame.Text = dtGame.ToString();
}
}
编辑:新增截图...
后仅一分钟=约3个月
只有四分钟=约1年
感谢您的评论,我还没有能够使它的工作,但我会再试一次;任何机会,我可以看到你的表单看起来像什么,你在“使用” – algoBaller 2013-05-14 21:44:09
它只是一个标准形式的定时器(timer1),一个按钮(button1)和三个标签(lblReference,lblReal,lblGame)。通过IDE将按钮Click()事件连接起来,运行它,然后单击按钮。你应该看到标签上的时间变化......这里没什么特别的。 – 2013-05-14 22:12:21
添加了两个屏幕截图。第一个是过了一分钟后,第二个过了四分钟就过了。 – 2013-05-14 22:24:22
1分钟后,实时等于三个月的游戏时间,您应该将间隔设置为1000 * 60. – nvoigt 2013-05-14 17:17:30
您是否在代码中的任何位置调用了AddTime()? – Renan 2013-05-14 17:18:16
好的,我会改变时间间隔。 @Ranan不,我没有,我不知道如何,那是我所有与计时器有关的代码。有什么建议么? – algoBaller 2013-05-14 17:22:43