如何使用 C# 实现延迟?
我想让一个应用程序执行几条指令来传递以下指令必须等待几毫秒。
这样:
while(true){
send("OK");
wait(100); //or such delay(100);
}
在 C# 中可能吗?
I want to make an application to execute several instructions to pass the following instruction has to wait a few milliseconds.
Such that:
while(true){
send("OK");
wait(100); //or such delay(100);
}
Is it possible in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Thread.Sleep(100);
会成功的。这可以在 System.Threading 命名空间中找到。Thread.Sleep(100);
will do the trick. This can be found in theSystem.Threading
namespace.您可以使用 Thread.Sleep() 方法将当前线程挂起 X 毫秒:
You can use the Thread.Sleep() method to suspend the current thread for X milliseconds:
要睡眠 100 毫秒,请使用
Thread.Sleep
To sleep for 100ms use
Thread.Sleep
您可以使用
Thread.Sleep
,但我强烈建议不要这么做,除非你 A) 知道它是合适的,并且 B) 没有更好的选择。如果您可以更具体地说明您想要实现的目标,可能会有更好的替代方案,例如使用 事件 和处理程序。
You can use
Thread.Sleep
, but I'd highly discourage it, unless you A) know for a fact it is appropriate and B) there are no better options. If you could be more specific in what you want to achieve, there will likely be better alternatives, such as using events and handlers.Thread.Sleep(毫秒) 应在那一秒停止应用程序。阅读 MSDN 中的 Thread.Sleep。
Thread.Sleep(milliseconds) should stop the application for that second. Read up on Thread.Sleep in MSDN.
是的,您可以使用 Thread.Sleep 方法:
然而,睡眠通常表明设计决策不理想。如果可以避免,我建议这样做(使用回调、订阅者模式等)。
Yes, you can use the Thread.Sleep method:
However, sleeping is generally indicative of a suboptimal design decision. If it can be avoided I'd recommend doing so (using a callback, subscriber pattern etc).
这将是一个更好的选择,因为它不会锁定当前的头并使其无响应。
创建这个方法
然后像这样调用
不要在调用延迟的方法中伪造异步!
了解更多信息请参阅我在尝试做类似事情时发现的此页面
This would be a much better option as it doesn't lock up the current thead and make it unresponsive.
Create this method
Then call like this
Don't forge the async in the method that calls the delay!
For more info see this page I found when trying to do a similar thing
Thread.Sleep()
是解决方案。它可用于等待一个线程,直到其他线程完成其工作 OR 在执行一条指令后,有人想要在执行之前等待一段时间后来的声明。它有两个重载方法。第一个重载方法采用 int 类型毫秒参数,第二个重载方法采用时间跨度参数。
1 毫秒=1/1000s
OR1 秒=1000 毫秒
假设如果有人想等待 10 秒,代码将是......
有关 Thread.Sleep() 的更多详细信息,请访问 http://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx
快乐编码......!
Thread.Sleep()
is the solution.It can be used to wait one thread untill other thread completes it's working OR After execution of one instruction someone wants to wait time before executing the later statement.It has two overloaded method.first take int type milisecond parameter while second take timespan parameter.
1 Milisecond=1/1000s
OR1 second=1000 Miliseconds
Suppose if someone wants to wait for 10 seconds code will be....
for more details about
Thread.Sleep()
please visit http://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspxHappy Coding.....!