简体   繁体   中英

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. 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. 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.

      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?. 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:

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.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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