-2

我看到这不会在指定时间等待。

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

4

3 回答 3

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;
        }
于 2019-06-11T17:07:53.993 回答
-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循环:


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

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

于 2019-06-11T17:31:08.077 回答
-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);
}
于 2019-06-11T18:13:47.750 回答