简体   繁体   English

可接受使用Thread.Sleep()

[英]Acceptable use of Thread.Sleep()

I'm working on a console application which will be scheduled and run at set intervals, say every 30 minutes. 我正在研究一个控制台应用程序,它将按照设定的时间间隔进行安排和运行,比如说每30分钟一次。 Its only purpose is to query a Web Service to update a batch of database rows. 其唯一目的是查询Web服务以更新一批数据库行。

The Web Service API reccommends calling once every 30 seconds, and timeout after a set interval. Web Service API重新命令每30秒调用一次,并在设置的间隔后超时。 The following pseudocode is given as an example: 以下伪代码作为示例给出:

listId := updateList(<list of terms>)
LOOP
  WHILE NOT isUpdatingComplete(listId)
END LOOP
statuses := getStatuses(“LIST_ID = {listId}”)

I have coded this roughly in C# as: 我在C#中大致编写了这个代码:

int callCount = 0;
while( callCount < 5 && !client.isUpdateComplete(listId, out messages) )
{
    listId = client.updateList(options, terms, out messages);
    callCount++;
    Thread.Sleep(30000);
}
// Get resulting status...

Is it OK in this situation to use Thread.Sleep()? 在这种情况下使用Thread.Sleep()可以吗? I'm aware it is not generally good practice but from reading reasons not to use it this seems like acceptable usage. 我知道这通常不是好的做法,但从阅读理由不使用它看起来似乎是可接受的用法。

Thanks. 谢谢。

Thread.Sleep ensures the current thread doesn't return until at least the specified milliseconds have passed. Thread.Sleep确保当前线程至少在指定的毫秒数后才返回。 There are plenty of places it's appropriate to do that, and your example seems fine, assuming it's running on a background thread. 有很多地方适合这样做,你的例子似乎很好,假设它在后台线程上运行。

Some example places you don't want to use it - on the UI thread or where you need to do exact timing. 一些示例您不想使用它 - 在UI线程上或您需要精确计时的位置。

Generally speaking, Thread.Sleep is like any other tool: perfectly OK to use, except when it's terribly misused. 一般来说, Thread.Sleep就像任何其他工具一样:完全可以使用,除非它被严重误用。 I disagree with the "not generally good practice" part, which is the result of people abusing Thread.Sleep when they should be doing something else (ie blocking on a synchronization object). 我不同意“通常不好的做法”部分,这是人们滥用Thread.Sleep的结果,当他们应该做其他事情时(即在同步对象上阻塞)。

In your case the program is single-threaded, it has no UI (ie the thread has no message loop) and you do not want to synchronize with external events. 在你的情况下,程序是单线程的,它没有UI(即线程没有消息循环),你不想与外部事件同步。 Therefore Thread.Sleep is just fine. 因此Thread.Sleep就好了。

The general objection against Sleep() is that it wastes a Thread. 对Sleep()的普遍反对意见是它浪费了一个线程。

In your case there is only 1 Thread (maybe 2) so that is not really a problem. 在你的情况下,只有1个线程(可能是2个),所以这不是一个真正的问题。

So I think it looks fine (but I would sleep 29 seconds to cut some slack). 所以我认为它看起来很好(但我会睡29秒来减少一些松弛)。

It's fine, except that you cannot interrupt it once it goes into sleep, without aborting the thread (which is not recommended). 这很好,除了你进入睡眠状态后不能打断它,而不会中止线程(不建议这样做)。

That's why a ManualResetEvent might be a better idea, since it can be signalled ("awaken") from a different thread. 这就是为什么ManualResetEvent可能是一个更好的主意,因为它可以从不同的线程发出信号(“唤醒”)。

you could stick with the Thread.Sleep method. 你可以坚持使用Thread.Sleep方法。 But it would be more elegant to schedule it to run every 30 minutes - so you don't have to take care of the waiting inside your application. 但是安排它每30分钟运行一次会更优雅 - 所以你不必在应用程序内部等待。

Thread.Sleep isn't the best for executing periodic logic. Thread.Sleep不是执行周期性逻辑的最佳选择。 Thread.Sleep(n) means your thread will relinquish control for n milliseconds. Thread.Sleep(n)表示你的线程将放弃控制n毫秒。 There is no guarantee that it will regain control after n milliseconds, it depends on the CPU load. 无法保证在n毫秒后它将重新获得控制权,这取决于CPU负载。

If you are locking the thread for 30 mins case you should schedule a windows task every 30 mins, so the program executes and then ends. 如果您将线程锁定30分钟,则应每30分钟安排一次Windows任务,以便程序执行然后结束。 That way you are not locking a thread for so long. 这样你就不会长时间锁定一个线程。

For shorter times, like 30 secs / 1 min, System.Thread.Sleep() is perfectly fine. 对于较短的时间,如30秒/ 1分钟,System.Thread.Sleep()完全没问题。 For more than 5 mins i would use a windows task. 超过5分钟我将使用Windows任务。 (Im spanish i think on the english version are called like that, im talking about the tasks you schedule from the control panel ;-) ) (我认为英语版本的西班牙语就像那样,我在谈论你从控制面板安排的任务;-))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM