c# - 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.
解决方案
你想继续你当前的使用模式Thread.Sleep(n)
,可以使用一个简单的循环。
while (DateTime.Now.Minute <=10)
{
Thread.Sleep(1000);
}
一般来说,虽然最好安排任务或至少使用异步而不是阻塞线程。
推荐阅读
- machine-learning - L1-regularization:在哪里使用惩罚成本函数?
- firebase - firebase 存储无法上传任何文件
- scala - Scala如何在某些覆盖条件下避免var
- java - 显示对话框时 JFileChooser 导致 UnsatisfiedLinkError
- amazon-web-services - AWS Batch 背后的调度策略
- python - 使用带有类方法的 theading.Thread 或 multiprocessing.Process 时是否需要将“self”传递给 args
- html - 如何在 Ionic 3 中从 HTML 调用函数?
- xsd - XSD 正则表达式匹配前 4 个字符
- javascript - 为什么使用 push 函数使数组变为数字?
- sass - scss bootstrap 4覆盖映射