I want to create a class that reads SMS messages from a GSM device.
I created a timer(system.threading) that reads for incoming message every second.
public void ReadMessage(){
//read sms messages
//and store it into the database
}
Sometimes ReadMessage() takes more than a second. How can I prevent the timer
from calling this procedure when the previous one is not yet finished?
1. Are AutoResetEvent and WaitOne good for this?
2. Is Threading.Timer a good choice? or should I do it on a single thread?
You should use a
System.Timers.Timer, which is easier to work with.(It’s a friendlier wrapper around
Threading.Timer)Set
AutoResettofalse, thenStart()the timer again at the end of the handler.Don’t use a dedicated thread; there’s no point in keeping a thread around doing nothing so that you can wake it up every second.