How do I prevent the timer from starting the first time at startup?
I have a timer that does a specific action when it starts (1000ms interval). Is there a way to prevent the timer from starting and go to the function the first time and start doing code instead?
private void timer_Tick(object sender, EventArgs e)
{
labelStatus.Text = "Waiting for next file" + "(" + (class.property / 1000).ToString("#0.##0") + " seconds)";
class.property -= 1000;
}
I want the timer to immediately start counting down (update the label) instead of waiting 1 second before updating the first label
source to share
What is stopping you from extracting the code fired on the Timer_Elapsed event and creating a separate procedure (aka Refactoring).
After your procedure will call it just before the timer starts.
For example, from this:
private void Timer_Elapsed(object sender, TimerElapsedEventArgs e)
{
int a = 0;
for(i=0; i<100;i++)
a += i;
}
to ....
private void Timer_Elapsed(object sender, TimerElapsedEventArgs e)
{
ExecuteCalc();
}
private void ExecuteCalc()
{
int a = 0;
for(i=0; i<100;i++)
a += i;
}
and in your code:
ExecuteCalc();
Timer1.Enabled = true;
source to share
You can simply "fake" a call to your ticker handler like this:
timer_Tick(null, null);
when you first start your timer. However, this is bad form and it is better to extract the code (refactoring) into a method and call the method. Then ask your handler for the same method as Steve:
private void timer_Tick(object sender, EventArgs e)
{
UpdateLabel();
}
private UpdateLabel()
{
labelStatus.Text = "Waiting for next file" + "(" + (class.property / 1000).ToString("#0.##0") + " seconds)";
class.property -= 1000;
}
Now call it the same way Jeff showed you:
UpdateLabel();
timer.Enable = true;
source to share