I have a Timer that polls an external service. The poll can take a while, so I use a one-shot Timer(System.Timers.Timer) and re-enable it when the operation completes, so I have only one running poll at a time. Now I need to stop this timer externally using the reference and prevent the event from restarting it in a separate thread.
System.Timers.Timer pollTimer = new System.Timers.Timer {
Enabled = true,
AutoReset = false,
Interval = 4 * 1000,
};
pollTimer.Elapsed += new System.Timers.ElapsedEventHandler(
(object sender, System.Timers.ElapsedEventArgs e) => {
if (!PollSuccess())
{
((System.Timers.Timer) sender).Start();
}
else
{
// Work
}
});
// I want the timer to stop permanently here, ie prevent the elapsed
//event handler from restarting it.
pollTimer.Stop();
Aucun commentaire:
Enregistrer un commentaire