开发者

C# regulate number of emails sent

I was wondering if anyone knows a good way to regulate how many emails are sent through C#?

开发者_Python百科

Here is my scenario. I have a windows service that monitors three other windows services. I am using a service controller to get the status of all 3 services and if the status of any of these services change to stopped, it sends an email. My issue is, I run this on a 60 second timer so it sends an email every 60 seconds until someone starts the service back up.

My first thought was, when first email is sent, create a text file and use a counter to number it. Do this while counter < 6 so I will only receive 5 emails max. I think this will work but it seems kind of silly.

Does anyone have an alternative to this or should I just go with my first thought and perform clean up on the files?

Thank you

EDIT: The reason that I was trying to limit the number of emails sent is because the people who receive these emails do not react very quickly. At the same time, those who handle Exchange do not want the service to spam people. I felt 5 would be enough to appease both sides.


I would suggest that you should track the down time of each service.

So every 60 seconds you check, if a service is down, store the DateTime that the service is down. The on the next 60 second interval you can check to see if the service was already down. i.e. you can tell if the service just went down or has been down a while. You can also add another flag to determine if the the last check was UP or DOWN.

Then when the program first finds the service down it can send the email. Once the service is back up it can reset this flag values so the next down time it knows to send a new email.

You can then also use these flags to delay email frequency if desired. Just add a new DateTime field for LastEmailSentTime and compare that with whatever interval you want for error emails (say 10 minutes)

Hope that gives you some ideas

EDIT: Some Sample...

bool ServiceWasDown = false;
DateTime ServiceDownTime = DateTime.Now;
DateTime LastEmailTime = DateTime.Now;

void OnTimerElapsed()
{
   if(IsServiceDown())
      ServiceDown();
   else
      ServiceUp();
}

void ServiceDown()
{
   if(ServiceWasDown)//already know about service
   {
      //if LastEmailTime more than 10 minutes ago send another email and update LastEmailTime
   }
   else//service just went down
   {
      //send new email
      LastEmailTime = DateTime.Now;
      ServiceWasDown = true;
      ServiceDownTime = DateTime.Now;
   }   
}

void ServiceUp()
{
   ServiceWasDown = false;   
}


If you use a System.Timers.Timer then You can add a int variable for count Elapsed events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜