首页 > 解决方案 > How to pause C# code for ten minutes at the start of each hour

问题描述

I'm trying to get my application to pause for nine to ten minutes at the start of each hour.

I've been able to get my application to pause for that amount of time

Random waitTime = new Random();
int milliseconds = waitTime.Next(3000, 5000);
System.Threading.Thread.Sleep(milliseconds);

I shortened the time in my code for testing purposes.

Now I need to tie that into something that checks if the current time is in between (4:00, 4:10) or (5:00, 5:10) or (21:00, 1:00) and resume if it is not.

标签: c#timerwait

解决方案


你想继续你当前的使用模式Thread.Sleep(n),可以使用一个简单的循环。

while (DateTime.Now.Minute <=10)
{
    Thread.Sleep(1000);
}

一般来说,虽然最好安排任务或至少使用异步而不是阻塞线程。


推荐阅读