替代Thread.sleep()方法用于模拟暂停 [英] Alternatives to Thread.Sleep() for simulating pauses

查看:790
本文介绍了替代Thread.sleep()方法用于模拟暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以Thread.sleep()方法是坏的(<一个href=\"http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx\">http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx).

So Thread.Sleep() is bad (http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx).

有没有推荐的替代方案在执行程序模拟暂停?例如,一个循环?虽然我想这涉及到大量的开销在初始化变量,检查布尔条件等。

Is there any recommended alternative to simulating a pause in execution of a program? Eg a loop? Although I suppose this involves a lot of overhead in initialising variables, checking the bool condition, etc.

感谢

推荐答案

如果你只打算的模拟的暂停(如用于测试目的),我想了Thread.Sleep是一个完全有效的做法。在另一方面,如果你实际上是在等待着什么,某种线程安全的信号机制会更好(检查继承<一个类型href=\"http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx\"><$c$c>WaitHandle).

If you are only going to simulate a pause (as in for test purposes), I think Thread.Sleep is a perfectly valid approach. On the other hand, if you are actually waiting for something, some sort of thread-safe signalling mechanism would be better (check the types that inherits WaitHandle).

这篇关于替代Thread.sleep()方法用于模拟暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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