简体   繁体   English

在Windows服务中使用Thread.Sleep()

[英]Using Thread.Sleep() in a Windows Service

I'm writing a windows service that needs to sleep for long periods of time (15 hrs is the longest it will sleep, 30 mins is the shortest). 我正在写一个需要长时间睡眠的Windows服务(15小时是最长的睡眠,30分钟是最短的)。 I'm currently using Thread.Sleep(calculatedTime) to put my code into sleep mode. 我目前正在使用Thread.Sleep(calculatedTime)将我的代码置于睡眠模式。 Is Thread.Sleep the best option or should I be using a timer? Thread.Sleep是最佳选择还是我应该使用计时器? I've been googling this for a while and can't find a concise answer. 我已经谷歌搜索了一段时间,无法找到简明的答案。 Since this is a windows service, I don't have to worry about locking the UI, so I can't think of a reason not to use Thread.Sleep. 由于这是一个Windows服务,我不必担心锁定UI,所以我想不出不使用Thread.Sleep的原因。

Any insight would be appreciated. 任何见解将不胜感激。

I would use a timer, Thread.Sleep, could cause a blocking piece that could prevent the service from shutting down. 我会使用一个计时器,Thread.Sleep,可能会导致阻止服务关闭的阻塞。

If the interval is that wide spread, and regular, you might just schedule it as well. 如果间隔是广泛的,并且是常规的,您也可以只安排它。 But if you are talking about long, non-consistent intervals, then yes a Timer would be better. 但是如果你谈的是长的,不一致的间隔,那么定时器会更好。

Since a service may be asked to stop at any time by the Service Control Manager, your thread should always be ready to respond to these requests, so you should not use Thread.Sleep(). 由于服务控制管理器可能会要求服务随时停止,因此您的线程应始终准备好响应这些请求,因此不应使用Thread.Sleep()。 Instead, create a manual-reset event in the main thread and use its WaitOne method with a timeout in your worker thread. 相反,在主线程中创建一个手动重置事件,并使用其WaitOne方法在工作线程中超时。 WaitOne will return false when the time expires. 当时间到期时,WaitOne将返回false。

When your service class's OnStop or OnShutdown methods are called, set the event and that will cause WaitOne to return true and you can then exit your worker thread. 调用服务类的OnStop或OnShutdown方法时,设置事件并使WaitOne返回true,然后退出工作线程。

It's generally regarded as bad practice to use Thread.Sleep() in a lot of cases. 在很多情况下,通常认为使用Thread.Sleep()是不好的做法。

If you want the service to run in the background, you should use a timer. 如果您希望服务在后台运行,则应使用计时器。

If the service only needs to be run at scheduled intervals, I'd recommend you look into using Windows task scheduler to allow Windows to run the application when you need it to. 如果服务只需要按计划的时间间隔运行,我建议您考虑使用Windows任务计划程序,以允许Windows在您需要时运行该应用程序。

You should not pre-calculate such large amounts of time and sleep for hours. 你不应该预先计算这么长的时间并且睡几个小时。 Sleep for a minute at best, then wake up and re-calculate the time, sleep again for no more that a minute. 最好睡一分钟,然后醒来并重新计算时间,再睡一分钟。 I assume the calculation is very cheap or it can be made very cheap with caching. 我假设计算非常便宜,或者可以通过缓存使其非常便宜。 The problem my advise is trying to mitigate is that computer clocks are surprisingly 'jumpy', mostly due to time drift corrected by network time service, also because of daylights savings and not least because user adjusting the clock. 我的建议试图缓解的问题是计算机时钟令人惊讶地“跳跃”,主要是由于网络时间服务纠正了时间偏差,也是因为节省了日光,尤其是因为用户调整时钟。 So is better to constantly recompute the time for such long intervals, even if it means waking up every minute or so. 因此,最好不断重新计算这么长时间间隔的时间,即使这意味着每分钟都会醒来。 And don't be surprised (ie. don't assert) if you wake up in the 'past', clocks can adjust back in time. 如果你在“过去”中醒来,时钟可以及时调整,也不要感到惊讶(即不要断言)。

Another thing to consider is that threads are finite resources and each thread consumes a portion of memory (1MB?) for the its stack. 另一件需要考虑的事情是线程是有限的资源,每个线程为其堆栈消耗一部分内存(1MB?)。 They may also increase load for the scheduler. 它们还可能增加调度程序的负载。

Now, if your service isn't doing much else the wasted space is trivial, but it is wise to be aware of this before you start allocating multiple threads. 现在,如果您的服务没有做太多其他浪费的空间是微不足道的,但在开始分配多个线程之前明智地注意这一点。 Using a ThreadPool and/or Timers is much more efficient. 使用ThreadPool和/或Timers效率更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Thread.Sleep时启动.NET Windows服务时出现问题 - Problems starting a .NET Windows Service when using Thread.Sleep Windows服务需要等待,Thread.Sleep? - Windows Service needs to wait, Thread.Sleep? Animation 与 windows forms 和数据网格使用 thread.sleep(int) - Animation with windows forms and datagrid using thread.sleep(int) 为什么 Thread.Sleep(... ) 避免 CPU 使用基于多线程 windows 服务的应用程序的 100% 繁重执行? - Why does Thread.Sleep( ... ) avoids CPU using 100% of a heavy execution from a multithread windows service based application? Windows Store中.NET中的Thread.Sleep替换 - Thread.Sleep replacement in .NET for Windows Store Thread.Sleep在Windows 8下无法与Silverlight 5一起使用 - Thread.Sleep Not Working with Silverlight 5 under Windows 8 在使用程序时使用Thread.Sleep - Using Thread.Sleep, while using program C#-Thread.Sleep()在我的Windows服务中似乎不起作用 - C# - Thread.Sleep() doesn't seem to work in my Windows Service Windows 服务停留在“启动”状态以及 Thread.Sleep() 和 Task.Delay() 之间的区别 - Windows service stuck in 'starting' state and difference between Thread.Sleep() and Task.Delay() 使用 Thread.Sleep 进行等待的替代方法 - Alternatives to using Thread.Sleep for waiting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM