在Windows服务使用Thread.sleep代码() [英] Using Thread.Sleep() in a Windows Service

查看:110
本文介绍了在Windows服务使用Thread.sleep代码()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写需要进行长时间睡一个窗口服务(15小时是它会睡觉的时间最长,30分钟是最短)。我目前使用的 Thread.sleep代码(calculatedTime)把我的代码进入休眠模式。是的Thread.Sleep最好的选择还是应该使用一个计时器?我一直在Google上搜寻这一段时间,并不能找到一个简洁的回答。由于这是一个窗口服务,我不必担心锁定用户界面,所以我不能想到一个理由不使用的Thread.Sleep。

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). I'm currently using Thread.Sleep(calculatedTime) to put my code into sleep mode. Is Thread.Sleep the best option or should I be using a timer? 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.

任何有识之士将不胜感激。

Any insight would be appreciated.

推荐答案

我会用一个定时器,Thread.sleep代码,可能导致挡片,可以防止从关闭的服务。

I would use a timer, Thread.Sleep, could cause a blocking piece that could prevent the service from shutting down.

如果该区间是广为流传,并定期,你可能只安排它。但是,如果你正在谈论长,非一致的时间间隔,然后是一个计时器会更好。

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.

这篇关于在Windows服务使用Thread.sleep代码()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆