Question

I am creating a windows service, which checks folders, other services ect. and sends alert textmessages if any of these are down. However, I am not interested in getting alert messages during a given period of time (from 23.00 evening to 07.00 morning).

The service is set up through an XML configuration file, where this period of silence can be predefined.

I need a method to check if the service is operating in the period of time, so it will suspend text messaging. The service checks for this period every 5 minutes, so I cannot figure out how to cope shifting of days, during these checks.

To elaborate:

My current code:

bool silende = false;
DateTime silenceModeFrom = new DateTime(
    DateTime.Now.Year, 
    DateTime.Now.Month, 
    DateTime.Now.Day, 
    int.Parse(doc.XPathSelectElement(
        "//SurveilanceServiceConfig/SilenceModeTimeFrom").Value), 
    00, 
    00);

DateTime silenceModeTo = silenceModeFrom.AddHours(
    int.Parse(doc.XPathSelectElement(
        "//SurveilanceServiceConfig/SilenceModeTimeHours").Value));

if (DateTime.Now > silenceModeFrom && DateTime.Now < silenceModeTo)
{
     silence = true;
}

In this code, the check works if it is made before 24.00, but not after as DateTime.Now.Day etc., will be the next day. So in theory, code only validates silence period as being from 23.00 to 24.00 per day.

Était-ce utile?

La solution

You aren't that far off...

There is a slight issue with your code as it stands, everytime you call DateTime.Now the time would have changed - you need to capture that instance of time when the service is triggered i.e.

var instant = DateTime.Now;
DateTime silenceModeFrom = new DateTime(instant.Year, instant.Month, instant.Day, int.Parse(doc.XPathSelectElement("//SurveilanceServiceConfig/SilenceModeTimeFrom").Value), 00, 00);
DateTime silenceModeTo = silenceModeFrom.AddHours(int.Parse(doc.XPathSelectElement("//SurveilanceServiceConfig/SilenceModeTimeHours").Value));
bool silence = instant.TimeOfDay >= silenceModeFrom.TimeOfDay && instant <= silenceModeTo;

Looking at this code though, your time window appears wrong. You aren't really interested in the Date at all here - it's really just the time. Therefore, anything between 23:00hrs - 07:00hrs is considered the "silent" period i.e.

int curHour = DateTime.Now.Hour;
int silenceStartHour = int.Parse(doc.XPathSelectElement("//SurveilanceServiceConfig/SilenceModeTimeFrom").Value);
int silenceEndHour = int.Parse(doc.XPathSelectElement("//SurveilanceServiceConfig/SilenceModeTimeHours").Value);
bool silence = curHour > silenceStartHour || curHour < silenceEndHour;

The logic for determining whether you are in the silent period can be improved slightly to account for a range that is all in one day, or a range that is split over midnight.

bool silence = silenceStartHour <= silenceEndHour
    ? (curHour >= silenceStartHour && curHour < silenceEndHour)
    : (curHour >= silenceStartHour || curHour < silenceEndHour);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top