Thread.sleep代码替换.NET为Windows应用商店商店、代码、sleep、Thread

2023-09-02 01:52:18 作者:每晚半夜偷菜,累不累。

了Thread.Sleep 似乎并没有在.NET的Windows应用商店的应用程序支持。

Thread.Sleep doesn't seem to be supported in .NET for Windows Store apps.

例如,本

            System.Threading.Thread.Sleep(1000);

将编译时针对任何.NET框架(2.0,3.5,4.0,4.5),而不是在对Windows应用商店的应用程序面向.NET(或便携式类库都作为目标4.5和存储)。

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).

System.Threading.Thread仍然存在,只是没有睡眠的方法。

System.Threading.Thread is still there, it just doesn't have the Sleep method.

我要有事耽搁几秒钟,我的应用程序,有没有合适的替代品?

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.

推荐答案

Windows应用商店的应用程序接受异步 - 和异步暂停是由提供Task.Delay.因此,一个异步方法中,你会写:

Windows Store apps embrace asynchrony - and an "asynchronous pause" is provided by Task.Delay. So within an asynchronous method, you'd write:

await Task.Delay(TimeSpan.FromSeconds(30));

...或者任何你想要的延迟。异步方法将持续30秒后,但该线程将的没有的阻塞,就像所有的计谋 EX pressions。

... or whatever delay you want. The asynchronous method will continue 30 seconds later, but the thread will not be blocked, just as for all await expressions.