Timer Events, Increments, and Resets in C#
A timer is being used in my C# application to determine if an expected event has occure开发者_StackOverflow中文版d in a timely fassion. This is how I am currently attempting to do this:
// At some point in the application where the triggering event has just occured.
// Now, the expected event should happen within the next second.
timeout = false;
timer1.Interval = 1000; // Set timeout for 1 second.
timer1.Start();
timer1_Tick(object sender, EventArgs e)
{
timeout = true;
}
// At some point in the application where the expected event has occured.
timer1.Stop();
// At a later point in the application where the timeout is
// checked, before procedding.
if ( timeout )
{
// Do something.
}
Now, what I am wondering is when the Start()
or Stop()
member methods are called, does that cause the timer count to reset? I am using Microsoft Visual C# 2008 Express Edition. Thanks.
When you call Stop() it effectively resets the timer back to 0, from the linked page:
Calling Start after you have disabled a Timer by calling Stop will cause the Timer to restart the interrupted interval. If your Timer is set for a 5000-millisecond interval, and you call Stop at around 3000 milliseconds, calling Start will cause the Timer to wait 5000 milliseconds before raising the Tick event.
From MSDN:
Calling Start after you have disabled a Timer by calling Stop will cause the Timer to restart the interrupted interval. If your Timer is set for a 5000-millisecond interval, and you call Stop at around 3000 milliseconds, calling Start will cause the Timer to wait 5000 milliseconds before raising the Tick event.
精彩评论