c#ATM模拟器中的定时器功能

本文关键字:定时器 功能 模拟器 c#ATM | 更新日期: 2023-09-27 18:28:47

嗨,我正在尝试使用c#中的计时器对象来更新我的数据库,如果卡在那里超过5秒,则将其设置为没收。我有点小麻烦。将在下方张贴我的代码

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);
}

c#ATM模拟器中的定时器功能

首先,您永远不会在计时器上设置间隔。

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();
    }
}