First of all, you can use System.Threading.Timer instead of this timer, because according to my experience, it is a more efficient timer (just advise personal experience).
Secondly, in such cases, you must specify a flag that is set after the previous timer completed the task (this flag is a static field that all threads access).
, - , reset , , - ( , , reset).
reset, , , .
, ( , ).
namespace SMSPicker
{
public partial class SMSPicker : ServiceBase{
SendSMS smsClass;
AutoResetEvent autoEvent;
TimerCallback timerCallBack;
Timer timerThread;
public SMSPicker()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
smsClass = new SendSMS();
autoEvent = new AutoResetEvent(false);
long timePeriod = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["timerDuration"]) ? 10000 : Convert.ToInt64(ConfigurationSettings.AppSettings["timerDuration"]);
timerCallBack = new TimerCallback(sendSMS);
timerThread = new Timer(timerCallBack, autoEvent, 0, timePeriod);
}
private void sendSMS(object stateInfo)
{
AutoResetEvent autoResetEvent = (AutoResetEvent)stateInfo;
smsClass.startSendingMessage();
autoResetEvent.Set();
}
protected override void OnStop()
{
smsClass.stopSendingMessage();
timerThread.Dispose();
}
}
}
namespace SMSPicker
{
class SendSMS
{
bool taskDone = true;
public SendSMS()
{
}
public void startSendingMessage()
{
if (!taskDone)
{
writeToLog("A Thread was already working on the same Priority.");
return;
}
try
{
}
catch (Exception ex)
{
writeToLog(ex.Message);
}
finally
{
taskDone = stopSendingMessage();
while (!taskDone)
{
taskDone = stopSendingMessage();
}
}
}
public bool stopSendingMessage()
{
bool smsFlagUpdated = true;
try
{
}
catch (Exception ex)
{
writeToLog(ex.Message);
}
return smsFlagUpdated;
}
}
}