0

I created .net Windows Service, and schedule it to run for every 5mins. This windows service contains the code that sends the emails and updates the table after sending the emails.

First I'm sending all the emails and then updating all the records in the db as bulk.So My question is I set the interval to 5mins, does it break my scenario like if it takes 5mins for sending emais,then timer stops with out updating the db, or it continues the execution until it completely execute the task.

_timer = new Timer(5* 60 * 1000);   
 _timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
_timer.AutoReset = true;
 _timer.Start();

........................

{//send one email at a time ..this is in loop
Library.SendEmail(edtls);   

ds.Tables["EDts"].Rows[i]["Sent"] = 1;
ds.Tables["EDts"].Rows[i]["Status"] = "Sent";   
ds.Tables["EDts"].Rows[i].EndEdit();
}..how about timer stop execution here(it sends the emails but doesn't update the table)
ds.Update(EDts, "EmailDts");//updating table here
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user8383606
  • 31
  • 1
  • 1
  • 6
  • _Which_ `Timer` class? .NET defines 4 different ones and it's possible to use an incorrect one –  Aug 16 '17 at 23:06
  • 2
    Possible duplicate of [Understanding working of Timer control in C#](https://stackoverflow.com/questions/8997838/understanding-working-of-timer-control-in-c-sharp) – Rufus L Aug 16 '17 at 23:11
  • It is not a control. AutoReset = true is quite dangerous and *will* cause the Elapsed event handler to run even when the previous tick isn't completed yet. That very rarely comes to a good end. Simple to fix, use false and set Enabled = true at the end of the event handler. Always use try/catch in the event handler to ensure exceptions are not swallowed. – Hans Passant Aug 17 '17 at 07:46

1 Answers1

0

Yeah so this might be tricky if sending the emails or the db updates take longer that the timer event triggering. You could stop the timer in between and continue it after.

_timer.Stop();
{//send one email at a time ..this is in loop
Library.SendEmail(edtls);   

ds.Tables["EDts"].Rows[i]["Sent"] = 1;
ds.Tables["EDts"].Rows[i]["Status"] = "Sent";   
ds.Tables["EDts"].Rows[i].EndEdit();
}..how about timer stop execution here(it sends the emails but doesn't update the table)
ds.Update(EDts, "EmailDts");//updating table here
_timer.Start();
Mitta
  • 443
  • 4
  • 12
  • All that is going to possibly lead to is undesirable _re-entrancy_ –  Aug 16 '17 at 23:16
  • I'll try below code and see _timer.Stop(); {//send one email at a time ..this is in loop Library.SendEmail(edtls); ds.Tables["EDts"].Rows[i]["Sent"] = 1; ds.Tables["EDts"].Rows[i]["Status"] = "Sent"; ds.Tables["EDts"].Rows[i].EndEdit(); }..how about timer stop execution here(it sends the emails but doesn't update the table) ds.Update(EDts, "EmailDts");//updating table here _timer.Start(); – user8383606 Aug 17 '17 at 17:26
  • Is it ok to call db update on each and every email sent because if i sent 100 emails and 90 emails are sent and remaining are having problem, it wont update the 90 emails also.Pls suggest. – user8383606 Aug 17 '17 at 18:24