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

时间:2012-03-21 18:27:54

标签: 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");
        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);
}

1 个答案:

答案 0 :(得分:0)

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

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秒钟过后,我们可以跳到多汁的东西:

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