通过比较和Thread.sleep代码为计时器延迟执行 [英] Compare using Thread.Sleep and Timer for delayed execution

查看:519
本文介绍了通过比较和Thread.sleep代码为计时器延迟执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应该运行在指定的时间量来延迟的方法。

I have a method which should be delayed running for a specified amount of time.

我应该使用

Thread thread = new Thread(() => {
    Thread.Sleep(millisecond);
    action();
});
thread.IsBackground = true;
thread.Start();

或者

Timer timer = new Timer(o => action(), null, millisecond, -1);

我已经读了一些<一个href=\"http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx\">articles关于使用了Thread.Sleep是糟糕的设计。但我真的不明白为什么。

I had read some articles about using Thread.Sleep is bad design. But I don't really understand why.

但对于使用定时器,定时器有dispose方法。由于执行被延迟,我不知道该如何处置定时器。你有什么建议吗?

But for using Timer, Timer has dispose method. Since the execution is delayed, I don't know how to dispose Timer. Do you have any suggestions?

或者,如果你有替代codeS为延迟执行也是AP preciate。

Or if you have alternative codes for delayed execution are also appreciate.

感谢您,

推荐答案

一个区别是System.Threading.Timer调度回调的线程池线程,而不是每次创建一个新线程。如果你需要给你的应用程序的生命周期内发生一次以上,这将节省创建和销毁一束线程的开销(这个过程是非常耗费资源,为您引用的文章指出),因为它会只需使用在池中的线​​程,如果你有一个以上的计时器一次就意味着你将有运行在一次(也节省了大量的资源)。更少的线程会

One difference is that System.Threading.Timer dispatches the callback on a thread pool thread, rather than creating a new thread every time. If you need this to happen more than once during the life of your application, this will save the overhead of creating and destroying a bunch of threads (a process which is very resource intensive, as the article you reference points out), since it will just reuse threads in the pool, and if you will have more than one timer going at once it means you will have fewer threads running at once (also saving considerable resources).

在换句话说,定时器将是更加高效。这也可能是更准确,因为Thread.sleep代码只保证至少只要等待为您指定的时间(操作系统可以把它睡觉更长的时间)的量。当然,计时器仍然不会是完全准确的,但其目的是火回调接近指定的时间越好,而这不一定了Thread.Sleep的意图。

In other words, Timer is going to be much more efficient. It also may be more accurate, since Thread.Sleep is only guaranteed to wait at LEAST as long as the amount of time you specify (the OS may put it to sleep for much longer). Granted, Timer is still not going to be exactly accurate, but the intent is to fire the callback as close to the specified time as possible, whereas this is NOT necessarily the intent of Thread.Sleep.

至于破坏定时器,回调可以接受一个参数,所以你可能能够通过自身的计时器作为参数,并在回调调用Dispose(虽然我没有尝试过这一点 - 我想这是可能计时器可能在回调过程中被锁定)。

As for destroying the Timer, the callback can accept a parameter, so you may be able to pass the Timer itself as the parameter and call Dispose in the callback (though I haven't tried this -- I guess it is possible that the Timer might be locked during the callback).

编辑:没有,我想你不能这样做,因为你必须在Timer构造本身指定回调参数

No, I guess you can't do this, since you have to specify the callback parameter in the Timer constructor itself.

也许这样的事情? (再次,还没有真正尝试过)

Maybe something like this? (Again, haven't actually tried it)

class TimerState
{
    public Timer Timer;
}

...并启动定时器:

...and to start the timer:

TimerState state = new TimerState();

lock (state)
{
    state.Timer = new Timer((callbackState) => {
        action();
        lock (callbackState) { callbackState.Timer.Dispose(); }
        }, state, millisecond, -1);
}

锁定应该试图释放之前,已设置计时器领域的定时器prevent计时器回调。

The locking should prevent the timer callback from trying to free the timer prior to the Timer field having been set.


附录:正如评论者指出,如果行动()做了与UI,然后使用一个System.Windows.Forms.Timer可能是一个更好的选择,因为它运行在UI线程上回调。然而,如果不是这种情况,它是向下的Thread.Sleep与Threading.Timer,Threading.Timer是要走的路。

Addendum: As the commenter pointed out, if action() does something with the UI, then using a System.Windows.Forms.Timer is probably a better bet, since it will run the callback on the UI thread. However, if this is not the case, and it's down to Thread.Sleep vs. Threading.Timer, Threading.Timer is the way to go.

这篇关于通过比较和Thread.sleep代码为计时器延迟执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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