1

I have a problem that min timer event is not fired when it should be, I set it to an interval of 500ms, but as I can see in my logg, this doesn't work as intended. I want the code to excecute when wait 0.5 seconds then start executing again!

 public void initilize_Rfid() { _timerComm = new System.Timers.Timer(500); _timerComm.Elapsed += CommTimerEvent; _timerComm.Enabled = true; } private void CommTimerEvent(object source, ElapsedEventArgs e) { try { _timerComm.Enabled = false; Logg("Inside CommTimerEvent " + _name); //TALKING TO A SERIALPORT AND DETERMENATING IF TO CONTINUE WITH THE TIMER if ((_continueTimer) { _timerComm.Enabled = true; Logg("Re-activating timer " + _name); } } catch (Exception ex) { Logg(ex.ToString()); } } 

I can see the text "Inisde ComTimerEvent TEST1", and then about 0.8 seconds later I get my "Re-activating timer TEST1". That is all just fine. But after this I get a up to 3 seconds delay until the timer fires again... Can I prevent this in some way?

4
  • System.Timers.Timer it has an AutoReset property. Set this to false and the timer will only run once. Then, in the timer method, start it again like you do. This way you don't need to stop/start the timer all the time. Commented Jan 23, 2014 at 12:48
  • Ok, but would that solve my problem? Since the problem is that the timer is not trigged at the right time even thou I set it to enable... Commented Jan 23, 2014 at 12:54
  • You can be sure the timer is stopped when the event handler is entered. You don't need to care about stopping it. The overhead of stopping/starting may influence the timer's behaviour. However, I've never ever noticed such problems when using System.Timers.Timer. Are you sure that you're not doing anything else after starting the timer? Are you sure that you understood that System.Timers.Timer events run in a different thread than the UI thread (exceptions might slow things down)? Commented Jan 23, 2014 at 12:56
  • I understand that it's a different thread, but I have no UI interaction in the timer. Commented Jan 23, 2014 at 13:02

2 Answers 2

1

Instead you can Use DispatcherTimer. The following code will help you to fire a method for every 5 seconds.

DispatcherTimer LabelTimer = new DispatcherTimer(); LabelTimer.Interval = TimeSpan.FromSeconds(5); LabelTimer.Tick += new EventHandler(TaskTimer_Tick_THROW); 
Sign up to request clarification or add additional context in comments.

2 Comments

This acctually did what I asked for, with this code it's triggered 0.5 seconds after I restart my timer!
Glad, It works for you!! you can accept the answer so that It would be helpful to others.
1

You're missing :

 _timerComm.Start(); 

this will start the timer. If you put it like this:

public void initilize_Rfid() { _timerComm = new System.Timers.Timer(500); _timerComm.Elapsed += CommTimerEvent; _timerComm.Enabled = true; _timerComm.Start(); } 

your code should work if you call this method.

1 Comment

The Enabled = true is equivalent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.