[英]disabling enabling internet access on a specific date using timer
在我的项目中,我可以在特定时间但在当前日期完全阻止Internet访问。 我通过使用计时器来做到这一点。 该计时器检查开始时间和结束时间,并使用用户指定的开始时间和结束时间每5秒控制一次以禁用和启用Internet连接。 现在,我不仅要针对当前日期在不同的日期中执行相同的操作。 我想通过计时器检查一天中每5秒的每一天,用户是否为该特定日期指定了禁用/启用时间间隔。
if (currentDate == chosenDate)
{
if ((currenttime >= starttime) && (currenttime <= endtime))
{
timerForHour.Enabled = true;
if (isConnectedToNetwork)
{
findNetworkName();
Disable(networkName);
}
}
else
{
exitFlag = true;
Enable(networkName);
}
}
else if(currentDate!=chosenDate)
{
}
这是我的代码,如果在其他if部分,我该怎么办? 谢谢。
我认为您正在以错误的方式进行操作。 不要以“今天是否存在”来思考,而应该以更一般的方式来思考:“是否匹配,如果匹配,那么我们是否在可接受的时间范围内?” 在不了解有关如何存储日期/时间范围的更多信息的情况下,我可以使用LINQ提供以下内容:
IsInternetAllowed = false;
DateTime dateOfInterest = datesToRestrict
.FirstOrDefault(dt => dt.Date == DateTime.Now.Date);
if (dateOfInterest != null)
{
if (timesToAllowDict.ContainsKey(DateTime.Now.Date))
{
List<Tuple<DateTime, DateTime>> lstAllowed =
timesToAllowDict[DateTime.Now.Date];
IsInternetAllowed = lstAllowed
.FirstOrDefault(time =>
DateTime.Now.Time > time.Value1 &&
DateTime.Now.Time < time.Value2) != null;
}
}
if (IsInternetAllowed)
Enable(networkName);
else
Disable(networkName);
Dictionary<DateTime, List<Tuple<DateTime, DateTime>>>
结构。 通过一些简化,您可能无需使用字典结构,而仅使用开始和结束时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.