等待n秒,那么接下来的code线不结冰形式 [英] Wait for n seconds, then next line of code without freezing form
问题描述
您好我想找到的移动到code的下一行,
Hi I am trying to find a method of waiting a number of milliseconds before moving to the next line of code,
我已经调查了Thread.Sleep但这将冻结的主要形式,谨以此保持活跃。
I have looked into Thread.Sleep but this will freeze the main form, I would like this to remain active.
我试过定时器和秒表和冻结双方的主要形式,当他们时,他们剔应该张贴到一个控制台。
I tried timers and stopwatches and both freeze the main form when they should be posting to a console when they tick.
我无法找到我想无论是等待使用task.delay或后台工作的一种方式。
I couldn't find a way of using task.delay or background worker in the wait I wanted either.
伪code:
Wait 2 - 6 seconds
Log "waiting"
Log "waiting"
Log "waiting"
Stop Waiting - Run next line of code.
我曾尝试这些方法只是冻结的形式和事后填写日志,我只是想等待不结冰的形式,也不必处理被称为事件的简单方法,将意味着下一行是无法运行。
The methods I have tried just freeze up the form and fill the log afterwards, I just want a simple method of waiting without freezing the form and without having to deal with events being called which would mean the next line isn't run.
因为我还是新的C#和它是推动我有点疯任何帮助将是真棒:(
Any help would be awesome because I am still new to c# and its been driving me a bit mad :(
推荐答案
的的await
关键字,会同 Task.Delay
使这个微不足道的。
The await
keyword, in conjunction with Task.Delay
makes this trivial.
public async Task Foo()
{
await Task.Delay(2000);
txtConsole.AppendText("Waiting...");
DoStuff();
}
这篇关于等待n秒,那么接下来的code线不结冰形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!