我怎样才能延迟 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9212966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i put a delay with C#?
提问by FredVaz
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#?
在 C# 中可能吗?
采纳答案by Neil Knight
Thread.Sleep(100);will do the trick. This can be found in the System.Threadingnamespace.
Thread.Sleep(100);会做的伎俩。这可以在System.Threading命名空间中找到。
回答by iefpw
Thread.Sleep(milliseconds) should stop the application for that second. Read up on Thread.Sleep in MSDN.
Thread.Sleep(milliseconds) 应该停止应用程序那一秒。阅读 MSDN 中的 Thread.Sleep。
回答by Christofer Eliasson
You can use the Thread.Sleep()method to suspend the current thread for X milliseconds:
您可以使用Thread.Sleep()方法将当前线程挂起 X 毫秒:
// Sleep for five seconds
System.Threading.Thread.Sleep(5000);
回答by Rich O'Kelly
Yes, you can use the Thread.Sleep method:
是的,您可以使用Thread.Sleep 方法:
while(true)
{
send("OK");
Thread.Sleep(100); //Sleep for 100 milliseconds
}
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).
然而,睡眠通常表明是次优的设计决策。如果可以避免,我建议这样做(使用回调、订阅者模式等)。
回答by Grant Thomas
You canuse 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 eventsand handlers.
您可以使用Thread.Sleep,但我非常不鼓励它,除非您 A) 知道它是合适的,并且 B) 没有更好的选择。如果您可以更具体地了解要实现的目标,那么可能会有更好的替代方案,例如使用事件和处理程序。
回答by SliverNinja - MSFT
To sleep for 100ms use Thread.Sleep
要休眠 100 毫秒,请使用 Thread.Sleep
While(true){
send("OK");
Thread.Sleep(100);
}
回答by Abdul
Thread.Sleep()is the solution.It can be used to wait one thread untill other thread completes it's working ORAfter 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/1000sOR1 second=1000 MilisecondsSuppose if someone wants to wait for 10 seconds code will be....
Thread.Sleep()是解决方案。它可用于等待一个线程,直到另一个线程完成它的工作或执行一条指令后,有人想在执行后面的语句之前等待时间。它有两个重载方法。第一个采用 int 类型的毫秒参数,第二个采用 timespan 参数。
1 Milisecond=1/1000s或者1 second=1000 Miliseconds假设如果有人想等待 10 秒,代码将是......
System.Threading.Thread.Sleep(10000);
for more details about Thread.Sleep()please visit http://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx
有关更多详细信息,Thread.Sleep()请访问http://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx
Happy Coding.....!
快乐编码.....!
回答by Notts90 supports Monica
This would be a much better option as it doesn't lock up the current thead and make it unresponsive.
这将是一个更好的选择,因为它不会锁定当前的 thead 并使其无响应。
Create this method
创建此方法
async System.Threading.Tasks.Task WaitMethod()
{
await System.Threading.Tasks.Task.Delay(100);
}
Then call like this
然后这样调用
public async void MyMethod()
{
while(true)
{
send("OK");
await WaitMethod();
}
}
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

