Thread.sleep代码替换.NET为Windows应用商店 [英] Thread.Sleep replacement in .NET for Windows Store

查看:181
本文介绍了Thread.sleep代码替换.NET为Windows应用商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了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应用商店的应用程序接受异步 - 和异步暂停是由提供<一href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.delay.aspx"><$c$c>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.

这篇关于Thread.sleep代码替换.NET为Windows应用商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆