简体   繁体   English

禁用使用计时器在特定日期启用Internet访问

[英]disabling enabling internet access on a specific date using timer

In my project I can block internet access completely in specific times but in a current date. 在我的项目中,我可以在特定时间但在当前日期完全阻止Internet访问。 I did this by using a timer. 我通过使用计时器来做到这一点。 This timer checks the start time and end time and also controls in every 5 seconds to disable and enable internet connection by using the start time and end time given by user. 该计时器检查开始时间和结束时间,并使用用户指定的开始时间和结束时间每5秒控制一次以禁用和启用Internet连接。 Now I want to do the same thing in different days not only for the current date. 现在,我不仅要针对当前日期在不同的日期中执行相同的操作。 I want to check every day in every 5 seconds of a day by timer if a disable/enable time interval is given by the user for that specific date. 我想通过计时器检查一天中每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)
        {

        }

This is my code what am I going to do in this else if section?. 这是我的代码,如果在其他if部分,我该怎么办? Thanks. 谢谢。

I think you are going about this the wrong way. 我认为您正在以错误的方式进行操作。 Don't think about it in terms of "if it's today or not", instead think about it in a more general way: "does the day match or not, if it does then are we within the time range acceptable?". 不要以“今天是否存在”来思考,而应该以更一般的方式来思考:“是否匹配,如果匹配,那么我们是否在可接受的时间范围内?” Without knowing more about how you are storing the day / time ranges you are using I can offer the following using LINQ: 在不了解有关如何存储日期/时间范围的更多信息的情况下,我可以使用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);


I make the following assumptions about your data: 我对您的数据做出以下假设:

  1. There are potentially multiple times allowed per day. 每天可能有多次允许。
  2. Any time that network access is not allowed it is disabled. 任何时候不允许网络访问都会被禁用。
  3. You store your allowed dates & times in a 您将允许的日期和时间存储在
    Dictionary<DateTime, List<Tuple<DateTime, DateTime>>> structure. Dictionary<DateTime, List<Tuple<DateTime, DateTime>>>结构。
  4. That structure represents Date, List of times allowed (start then end). 该结构表示日期,允许的时间列表(开始然后结束)。
  5. Each day is added separately (one dictionary key per day allowed). 每天分别添加(每天允许一个字典密钥)。

It is likely that with some streamlining you could remove the need to have a dictionary structure and only use the start & end times instead. 通过一些简化,您可能无需使用字典结构,而仅使用开始和结束时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM