当条件为假时转义while循环

时间:2019-06-11 17:02:12

标签: c# datetime wait dayofweek andand

我看到这在指定时间内没有等待。

我一直在尝试学习c#,但还不完全了解所有语法。我只想知道是否有更好的方法来完成此任务。

while (DateTime.Now.DayOfWeek == DayOfWeek.Saturday 
    && DateTime.Now.DayOfWeek == DayOfWeek.Sunday 
    && DateTime.Now.Hour <=8 
    && DateTime.Now.Hour >=18  
    && DateTime.Now.Minute <=9)
{
    Random waitTime = new Random();
    int milliseconds = waitTime.Next(120000, 180000);
    System.Threading.Thread.Sleep(milliseconds);
}

我希望它在周六或周日等待,如果是在0800之前,1800之后或在每小时的前十分钟。

问题是,我已经看到它在星期一的7:05 pm退出循环,这是两个条件。我是否正确使用&&函数,还是应该用其他东西代替它?

3 个答案:

答案 0 :(得分:2)

JonH是正确的,不能同时是星期六和星期日。这是您的代码的样子

            Random myRand = new Random();
        bool isSatOrSun = DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday;
        bool isNotBetween8And1800 = DateTime.Now.Hour <= 8 || DateTime.Now.Hour >= 18;
        bool isFirstTenMinutes = DateTime.Now.Minute <= 9;

        //if you want to do Between 8 & 18 on Saturday/Sundays OR first 10 minutes of each hour on Saturday/Sunday

        while(isSatOrSun && (isNotBetween8And1800 || isFirstTenMinutes))
        {
            //do your sleep
            int waitTime = myRand.Next(120000, 180000);
            System.Threading.Thread.Sleep(waitTime);

            //now recheck your variables
            isSatOrSun = DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday;
            isNotBetween8And1800 = DateTime.Now.Hour <= 8 || DateTime.Now.Hour >= 18;
            isFirstTenMinutes = DateTime.Now.Minute <= 9;
        }


        //if you want to do all day on Saturday/Sundays OR Between 8 & 18 any day of the week OR  first 10 minutes of each hour any day of the week

        while (isSatOrSun || isNotBetween8And1800 || isFirstTenMinutes)
        {
            //do your sleep
            int waitTime = myRand.Next(120000, 180000);
            System.Threading.Thread.Sleep(waitTime);

            //now recheck your variables
            isSatOrSun = DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday;
            isNotBetween8And1800 = DateTime.Now.Hour <= 8 || DateTime.Now.Hour >= 18;
            isFirstTenMinutes = DateTime.Now.Minute <= 9;
        }

答案 1 :(得分:-1)

既然您正在学习,我建议您为逻辑使用布尔方法,并在while函数上调用它,如下所示:

方法:

public static bool IsWaitingTime()
{
 if ( ( DateTime.Now.DayOfWeek == DayOfWeek.Saturday  || DateTime.Now.DayOfWeek == DayOfWeek.Sunday )
 && ( DateTime.Now.Hour <= 8 || DateTime.Now.Hour >= 18 ) 
 && DateTime.Now.Minute <= 9 ) 
   return true;
else
   return false;
} 

循环时


while ( IsWaitingTime )
    {
        Random waitTime = new Random();
        int milliseconds = waitTime.Next(120000, 180000);
        System.Threading.Thread.Sleep(milliseconds);
    }

这是一种方法,正如@JonH所说,日期不能同时是星期六和星期日(至少在同一时区)。 chris-crush-code的答案也正确。

答案 2 :(得分:-2)

根据我过去20分钟的经验,如果满足以下任何条件,这似乎会暂停; 是星期天吗 是星期六吗 是早于0800, 是1800年以后 分钟是否小于10

Random waitTime = new Random();
while (DateTime.Now.DayOfWeek == DayOfWeek.Saturday || DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.Hour <=8 || DateTime.Now.Hour >=18  || DateTime.Now.Minute <=9)
{
int milliseconds = waitTime.Next(120000, 180000);
System.Threading.Thread.Sleep(milliseconds);
}