Martius Umali
Martius Umali

Reputation: 122

Single output on timer c#

I'm a beginner and trying to understand what timer is and how can I use it. I'll be really grateful to you if you replied to my question.

I have a timer called "Elapsed_Time" and I set the interval to 1000 millisecond. What I wanted to achieve is to show my message: "Hi just once" just once instead of showing it for every 1 second.

       private void Elapsed_Time_Tick(object sender, EventArgs e)
       {
         Messagebox.show("Hi just once");
       }

Upvotes: 1

Views: 78

Answers (2)

private void Elapsed_Time_Tick(object sender, EventArgs e)
{   
    Messagebox.show("Hi just once");
    Elapsed_Time_Tick.Enabled = false;      
}

You can do like this

Upvotes: 0

mxmissile
mxmissile

Reputation: 11681

if you still want the timer's Tick event to fire then try this...

private bool _hasTicked = false;

private void Elapsed_Time_Tick(object sender, EventArgs e)
{
     if(!_hasTicked)
     {
         Messagebox.show("Hi just once");
         _hasTicked = true;
     }
}

Upvotes: 3

Related Questions