在条件为假时转义 while 循环

Escaping while loop, while conditions are false

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

我一直在尝试学习 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:05pm 退出循环,这是两个条件。我是否正确使用了 && 函数,还是应该用其他东西替换它?

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;
        }

既然你正在学习,我建议你为你的逻辑使用布尔方法并在 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 循环:


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

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

根据我过去 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);
}