Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have two timers in a windows form app.

I want first timer1 to enable by button1 click.

What i want to know is how to do following:

The first timer1 must fire once(tick once).

And the second timer2 to start after 20 seconds of timer1 tick.

Thank you.
Posted
Updated 19-Jun-14 21:21pm
v2
Comments
DamithSL 20-Jun-14 3:22am    
can you update the question with the code that you tried so far?
Prasad Avunoori 20-Jun-14 3:27am    
Do you want both timers should fire only once?
Member 10579673 20-Jun-14 3:44am    
No the timer1 must fire once and after 20 seconds after Timer1 tick timer2 must start.interval of timer2 is 4000.

1. Start the second timer in timer1 after 20 seconds using sleep() method.
2. Stop the timer1 in timer2.

C#
int count = 1;

       private void button1_Click(object sender, EventArgs e)
       {
           timer1.Start();
           textBox1.Text = "Button1 Clicked at " + DateTime.Now.ToString();

       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           if (count == 1)
           {
               textBox1.Text = textBox1.Text + Environment.NewLine + "Timer1 Enabled at " + DateTime.Now.ToString();
           }

           if (count == 17)
           {
               timer2.Start();
           }
           count++;
       }

       private void timer2_Tick(object sender, EventArgs e)
       {
           timer1.Stop();
           textBox1.Text = textBox1.Text + Environment.NewLine + "Timer2 Enabled at " + DateTime.Now.ToString();

       }
   }



Interval of timer1 is 1000 and timer2 is 4000.
 
Share this answer
 
v3
Comments
Member 10579673 20-Jun-14 3:41am    
@Prasad Avunoori can ya please elaborate?
Prasad Avunoori 20-Jun-14 5:10am    
View my updated solution.
Kim Togo 20-Jun-14 6:59am    
My vote of 1.
"System.Threading.Thread.Sleep(20000);" it freezes the whole UI!

It will make the UI unresponsive for 20 sec.
Prasad Avunoori 20-Jun-14 7:53am    
Please have a look at my v3 of my solution.
Kim Togo 20-Jun-14 7:56am    
That is much better.
A good start is to look at the Timer Class[^] and see the examples. It will give you a good idea on how-to make it work.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900