Reputation: 778
So I have a Timer object that I'm going to call time, and time has a tick event that is called after 60 seconds are passed. I'm trying to use the time.Stop() method, but the time_Tick() is still called. How do I prevent the Tick method from occuring? Once the Timer has started is there no way of backing out? Thanks! I'm new to Tick events.
Upvotes: 2
Views: 1522
Reputation: 171218
Timer ticks can arrive concurrently and after the timer has been stopped (for fundamental reasons). Ticks could be queued right before you stop the timer.
If you want to reliably stop a timer set a boolean flag and inspect that flag in the tick event handler. If it's set ignore the tick.
You need to synchronize access to that flag.
Upvotes: 3
Reputation: 1520
If you have other timer objects you may be seeing this:
Calling Stop on any Timer within a Windows Forms application can cause messages from other Timer components in the application to be processed immediately, because all Timer components operate on the main application thread. If you have two Timer components, one set to 700 milliseconds and one set to 500 milliseconds, and you call Stop on the first Timer, your application may receive an event callback for the second component first. If this proves problematic, consider using the Timer class in the System.Threading namespace instead.
Upvotes: 1