Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用使用计时器在特定日期启用internet访问_C# - Fatal编程技术网

C# 禁用使用计时器在特定日期启用internet访问

C# 禁用使用计时器在特定日期启用internet访问,c#,C#,在我的项目中,我可以在特定时间完全阻止互联网访问,但在当前日期。 我是用定时器做的。此计时器检查开始时间和结束时间,并且每5秒控制一次,以使用用户给定的开始时间和结束时间禁用和启用internet连接。现在,我想在不同的日子里做同样的事情,而不仅仅是在当前日期。我想通过计时器在一天中每5秒检查一天,如果用户给出了该特定日期的禁用/启用时间间隔 if (currentDate == chosenDate) { if ((currenttime &

在我的项目中,我可以在特定时间完全阻止互联网访问,但在当前日期。 我是用定时器做的。此计时器检查开始时间和结束时间,并且每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(currentDate==chosenDate)
{

如果((currenttime>=starttime)&(currenttime我认为你的做法是错误的。不要用“是否是今天”来考虑它,而是用一种更一般的方式来考虑它:“这一天是否匹配,如果匹配,那么我们在时间范围内可以接受吗?”。在不了解如何存储您使用的日期/时间范围的情况下,我可以使用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);
IsInternetAllowed=false;
DateTime dateOfInterest=datesToRestrict
.FirstOrDefault(dt=>dt.Date==DateTime.Now.Date);
if(感兴趣的日期!=null)
{
if(timesToAllowDict.ContainsKey(DateTime.Now.Date))
{
允许的列表长度=
timesToAllowDict[DateTime.Now.Date];
IsInternetAllowed=lstAllowed
.FirstOrDefault(时间=>
DateTime.Now.Time>Time.Value1&&
DateTime.Now.Time

我对您的数据做出以下假设:

  • 每天可能允许多次
  • 任何不允许网络访问的时间都将被禁用
  • 您将允许的日期和时间存储在
    字典
    结构
  • 该结构表示日期、允许的时间列表(开始然后结束)
  • 每天单独添加(每天允许一个字典键)
  • 通过一些简化,您可能不需要使用字典结构,而只使用开始和结束时间