how to display a message box when timer elapses in

2019-03-04 07:07发布

问题:

I gave my time interval as 100. when the timer elapsed the message box is displayed but my screen is flooded with message boxes. How should i stop it with one message box indicating timer elapsed. Here's my code... Can you guide me where to give stop timer...

namespace timer1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Timer t = new Timer();
            t.Interval = 100;
            timer1.Enabled = true;
            timer1.Tick += new System.EventHandler(OnTimerEvent);

        }
        private void OnTimerEvent(object sender, EventArgs e)
        {

            MessageBox.Show("time over");

        }

    }
}

回答1:

I guess (as you don't provide code), in the Interrupt Service Routine:

public void YourTimer_Tick(object sender, EventArgs e)
{
      YourTimer.Stop();
      MessageBox.Show("You message...");
}


标签: c# timer