1

I have a little problem here. For now I'm starting one timer at the start of application using this:

if (Initialized == 0)
{
    errorCheckTimer.Elapsed += (sender, e) => OnErrorCheckTimerElapsed(sender, e);
    Initialized = 1;
}

It makes sure it's started only one time. If I were to execute this line more than once, the timer would fire multiple times and once.

Now after initializing the timer, I occassionally run it using:

errorCheckTimer.Interval = ErrorTimerInterval;
errorCheckTimer.Enabled = true;
errorCheckTimer.Start();

And after certain actions happen, I stop it with:

tradeErrorTimer.Enabled = false;
tradeErrorTimer.Stop();

But there is some obscure glitch happening. The timer always works when its firstly initialized, but sometimes will not turn on at all, and the OnErrorCheckTimerElapsed will not get executed.

Therefore to make it more reilable, can I remove the timer that has been added into OnErrorCheckTimerElapsed.Elapsed to initialize a new one every time I need to start it?

If I try to initialize them multiple times, I get the OnErrorCheckTimerElapsed firing multiple times at once.

Thank you.

Agent Smith
  • 49
  • 1
  • 6
  • Can you show a short but complete program demonstrating the problem? Also, your subscription would be simpler as just `errorCheckTimer.Elapsed += OnErrorCheckTimerElapsed;` – Jon Skeet Aug 20 '13 at 05:58
  • You could use `errorCheckTimer.Elapsed -= OnErrorCheckTimerElapsed` to remove the event handler to prevent it firing multiple times. However I would spend more time working out why you have to put this workaround in. – Ed W Aug 20 '13 at 06:03
  • @EdW, removing the handler that way would not work. When it was added like `errorCheckTimer.Elapsed += OnErrorCheckTimerElapsed`, the C# compiler generated an implicit delegate, i.e., `errorCheckTimer.Elapsed += new OnErrorCheckTimerElapsed(OnErrorCheckTimerElapsed)`. To remove it, one would need to add it explicitly and keep the track of it. – noseratio Aug 20 '13 at 06:11
  • I have tried doing it with the -= and it still had it firing multiple times. – Agent Smith Aug 20 '13 at 06:17
  • @Noseratio are you sure? http://stackoverflow.com/questions/1307607/removing-event-handlers – Ed W Aug 20 '13 at 06:27
  • @EdW, not so sure now :) I'd trust more the link you posted, but I'll check. – noseratio Aug 20 '13 at 06:37
  • @EdW, you are right - verified - thank you! Let's say, I lived under a rock and was unaware of this symmetric in syntactic convenience. Gotta check the IL code behind that. – noseratio Aug 20 '13 at 06:49
  • @AgentSmith can you add the event with the code `errorCheckTimer.Elapsed += OnErrorCheckTimerElapsed;` (not `errorCheckTimer.Elapsed += (sender, e) => OnErrorCheckTimerElapsed(sender, e);`) and remove it with `errorCheckTimer.Elapsed -= OnErrorCheckTimerElapsed` and let me know the results? Or you can even remove it in the OnErrorCheckTimerElapsed method with `((Timer)sender).Elapsed -= OnErrorCheckTimerElapsed;`. If that doesn't work you'll have to post a complete sample demonstrating the issue as @JonS mentioned – Ed W Aug 20 '13 at 06:52
  • Also my elapsed function looks like `private void OnErrorCheckTimerElapsed(object source, ElapsedEventArgs e)` is that correct? – Agent Smith Aug 20 '13 at 08:02
  • Looks like `errorCheckTimer.Elapsed += OnErrorCheckTimerElapsed;` and `errorCheckTimer.Elapsed -= OnErrorCheckTimerElapsed;` did the trick. Post your explanation as an answer if you want so I can accept it and you'll get the points or whatever stackoverflow offers :P – Agent Smith Aug 20 '13 at 16:02

1 Answers1

0

If you don't want to run your timer more then once, you should set AutoReset property to false. errorCheckTimer.AutoReset = false;

Max
  • 1