C# 用于 Windows 应用商店的 .NET 中的 Thread.Sleep 替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12641223/
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
Thread.Sleep replacement in .NET for Windows Store
提问by Max
Thread.Sleepdoesn't seem to be supported in .NET for Windows Store apps.
.NET 似乎不支持Thread.Sleepfor Windows Store 应用程序。
For example, this
例如,这
System.Threading.Thread.Sleep(1000);
will compile when targeting any .NET Framework (2.0, 3.5, 4.0, 4.5), but not when targeting .NET for Windows Store apps (or in a portable class library which targets both 4.5 and store).
将在面向任何 .NET Framework(2.0、3.5、4.0、4.5)时编译,但在面向 Windows 应用商店应用程序(或同时面向 4.5 和商店的可移植类库)的 .NET 时不会编译。
System.Threading.Thread is still there, it just doesn't have the Sleep method.
System.Threading.Thread 仍然存在,只是没有 Sleep 方法。
I need to delay something for a few seconds in my app, is there a suitable replacement?
我需要在我的应用程序中延迟几秒钟,有合适的替代品吗?
EDIT why the delay is needed: My app is a game and the delay is to make it look like the computer opponent is "thinking" about his next move. The method is already called asynchronously (main thread isn't blocked), I just want to slow the response time down.
编辑为什么需要延迟:我的应用程序是一个游戏,延迟是为了让计算机对手看起来像是在“考虑”他的下一步行动。该方法已经被异步调用(主线程未被阻塞),我只想减慢响应时间。
采纳答案by Jon Skeet
Windows Store apps embrace asynchrony - and an "asynchronous pause" is provided by Task.Delay. So within an asynchronous method, you'd write:
Windows 应用商店应用程序包含异步 - 并且“异步暂停”由Task.Delay. 所以在异步方法中,你会写:
await Task.Delay(TimeSpan.FromSeconds(30));
... or whatever delay you want. The asynchronous method will continue 30 seconds later, but the thread will notbe blocked, just as for all awaitexpressions.
...或任何你想要的延迟。异步方法会在 30 秒后继续,但线程不会被阻塞,就像所有await表达式一样。
回答by igrimpe
There is almost NO reason (except for testing purposes) to EVER use Thread.Sleep().
几乎没有理由(除了测试目的)永远使用Thread.Sleep().
IF (and only if) you have a very good reason to send a thread to sleep, you might want to check Task.Delay(), which you can await to "wait" for a specified time. Though it's never a good idea to have a thread sitting around and do nothing. Bad practise ...
如果(且仅当)您有充分的理由让线程进入休眠状态,您可能需要检查Task.Delay(),您可以等待“等待”指定时间。尽管让线程闲置而无所事事从来都不是一个好主意。不好的做法...
回答by Benny Neugebauer
MainPage.xaml.cs
主页.xaml.cs
public MainPage()
{
this.InitializeComponent();
this.WaitForFiveSeconds();
}
private async void WaitForFiveSeconds()
{
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(5));
// do something after 5 seconds!
}
回答by Johannes Egger
I just had the same problem and found another interesting solution that I wanted to share with you. If you really want to block the thread I would do it like this (thanks @Brannon for the "slim" hint):
我刚刚遇到了同样的问题,并找到了另一个有趣的解决方案,我想与您分享。如果你真的想阻塞线程,我会这样做(感谢@Brannon 的“苗条”提示):
// `waitHandle.Set` is never called, so we wait always until the timeout occurs
using (var waitHandle = new ManualResetEventSlim(initialState: false))
{
waitHandle.Wait(TimeSpan.FromSeconds(5));
}
回答by Antony Thomas
Hate to state the obvious but in case anybody wanted a single line System.Threading.Tasks.Task.Delay(3000).Wait()
不想说明显的,但以防万一有人想要一条线 System.Threading.Tasks.Task.Delay(3000).Wait()

