c#ATM模拟器中的定时器功能
您好,我正在尝试使用c#中的计时器对象更新我的数据库,如果卡在那里超过5秒,则将其设置为没收。我有点小麻烦。我会把我的代码贴在下面c#ATM模拟器中的定时器功能,c#,timer,C#,Timer,您好,我正在尝试使用c#中的计时器对象更新我的数据库,如果卡在那里超过5秒,则将其设置为没收。我有点小麻烦。我会把我的代码贴在下面 private void timer1_Tick(object sender, EventArgs e) { if (seconds > 5) { timer1.Enabled = false; MessageBox.Show("Card NOT removed in time: CONFISCATED");
private void timer1_Tick(object sender, EventArgs e)
{
if (seconds > 5)
{
timer1.Enabled = false;
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
}
private void Form1_load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public void cardConfiscated(string number)
{
atmCardsTableAdapter1.confiscated(number);
atmCardsTableAdapter1.FillByNotConfiscated(boG_10033009DataSet.ATMCards);
}
首先,你从来没有设置过计时器的时间间隔
private void Form1_load(object sender, EventArgs e)
{
timer1 = new Timer(5000); // sets interval to 5 seconds
timer1.Elapsed += new new ElapsedEventHandler(timer1_Tick);
timer1.Start();
}
我在上面做了一个5秒的间隔,这样我们就不必多次调用计时器,当5秒过去时,我们可以直接跳到有趣的东西:
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
最后,您应该注意,如果计时器的间隔较短,则需要增加秒数,例如:
private void Form1_load(object sender, EventArgs e)
{
timer1 = new Timer(1000); // sets interval to 1 second
timer1.Elapsed += new new ElapsedEventHandler(timer1_Tick);
timer1.AutoReset = true; // sets the timer to restart after each run
timer1.Start();
}
然后我们需要像你一样,每间隔增加秒数
private void timer1_Tick(object sender, EventArgs e)
{
seconds++;
if (seconds > 5)
{
timer1.Stop();
MessageBox.Show("Card NOT removed in time: CONFISCATED");
login.cardConfiscated(cardNumber);
login.Visible = true;
this.Close();
}
}
在哪里定义了
seconds
?这是家庭作业吗?你还应该说明你遇到了什么麻烦(除了“小”)。在你的示例中,你的seconds变量没有在任何地方创建或递增,你缺少一些我们需要帮助解决问题的代码。谢谢你的快速响应。我为我的无知而道歉,我是c#编程的新手。我将秒定义为整数。私有整数秒=5;seconds
是否始终==5?如果是这样,seconds>5
将永远不会是真的,您描述的行为完全有道理