may try the following:
timer.Tick += Timer_Tick; timer.Interval = 0; timer.Start(); //... public void Timer_Tick(object sender, EventArgs e) { if (timer.Interval == 0) { timer.Stop(); timer.Interval = SOME_INTERVAL; timer.Start(); return; } //your timer action code here }
Another way could be to use two event handlers (to avoid an "if" check on every tick):
timer.Tick += Timer_TickInit; timer.Interval = 0; timer.Start(); //... public void Timer_TickInit(object sender, EventArgs e) { timer.Stop(); timer.Interval = SOME_INTERVAL; timer.Tick += Timer_Tick(); timer.Start(); } public void Timer_Tick(object sender, EventArgs e) { //your timer action code here }
However, a cleaner way is what has already been suggested:
timer.Tick += Timer_Tick; timer.Interval = SOME_INTERVAL; SomeAction(); timer.Start(); //... public void Timer_Tick(object sender, EventArgs e) { SomeAction(); } public void SomeAction(){ //... }
George Birbilis
source share