Reputation: 15
I call Stop() in the Elapsed event and do my work, then I call Start() after my work is finished, but the Elapsed event fires again when my work is still processing. This duplicate firing only happens at the first time the WinForm starts running, and won't happen again. I've already searched for this issue and the other one, but they didn't have the same problem as mine. Here's my sample code:
public partial class Form1 : Form {
private System.Timers.Timer timerRun;
public Form1() {
InitializeComponent();
Logger.Write(""); // Logger is my static class for writing logs
Logger.Write("************* Start Program *************");
timerRun = new System.Timers.Timer();
timerRun.Interval = 3000;
timerRun.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
timerRun.AutoReset = false;
}
private void Form1_Load(object sender, EventArgs e) {
timerRun.Start();
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
timerRun.Stop();
if (timerRun.Interval < 5000) {
timerRun.Interval = 5000;
}
DoWork();
timerRun.Start();
}
private void DoWork() {
try {
string guid = Guid.NewGuid().ToString();
Logger.Write("My Work Start " + guid);
// do some work that takes about 30 seconds to finish.
Logger.Write("My Work Finish " + guid);
} catch (Exception ex) {
// some action
}
}
}
I expect to get log like this:
00:00:00 [Debug] ->
00:00:00 [Debug] -> ************* Start Program *************
00:00:03 [Debug] -> My Work Start (guid-A)
00:00:33 [Debug] -> My Work Finish (guid-A)
00:00:38 [Debug] -> My Work Start (guid-B)
00:01:08 [Debug] -> My Work Finish (guid-B)
00:01:13 [Debug] -> My Work Start (guid-C)
00:01:43 [Debug] -> My Work Finish (guid-C)
but I actually got log like this:
00:00:00 [Debug] ->
00:00:00 [Debug] -> ************* Start Program *************
00:00:03 [Debug] -> My Work Start (guid-A)
00:00:08 [Debug] -> My Work Start (guid-B) // ????
00:00:33 [Debug] -> My Work Finish (guid-A)
00:00:38 [Debug] -> My Work Finish (guid-B) // ???
00:00:38 [Debug] -> My Work Start (guid-C)
00:01:08 [Debug] -> My Work Finish (guid-C)
00:01:13 [Debug] -> My Work Start (guid-D)
00:01:43 [Debug] -> My Work Finish (guid-D)
How on earth the guid-B event triggered? This won't happen if my work takes less than 5 seconds to finish.
Upvotes: 1
Views: 337
Reputation: 7440
Seems like you are running into the note from the documentation
see here:
https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer.interval?view=net-7.0#remarks (at the very bottom)
Note
If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. To set the interval without raising the event, you can temporarily set the Enabled property to true, set the Interval property to the desired time interval, and then immediately set the Enabled property back to false.
Edit:
Tested the described work around on my side and it seems to work.
(I added a Thread.Sleep(10_000);
in the DoWork
method)
Without temporarily setting Enabled, I am getting the undesired behahiour
With the work around
Upvotes: 3