线程睡眠在 Windows Phone 7 上运行不佳 [英] Thread sleep doesn't work well on Windows Phone 7

查看:21
本文介绍了线程睡眠在 Windows Phone 7 上运行不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的程序休眠几秒钟.当我使用 thread.sleep(x) 时,整个程序没有响应.根据这篇文章 -> http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx 不负责 3 秒的应用程序未通过认证:/(我必须等待 5 秒).

I want to put my program to a sleep for a couple of seconds. When I use thread.sleep(x) entire program doesn't respond. According to this article -> http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx application which is not responsible for 3 seconds doesn't pass the certification :/ (I have to wait 5 seconds).

推荐答案

你需要重新思考暂停时你想做什么.鉴于我们没有太多关于您尝试通过此暂停实现什么的信息,这有点难说,但我将在这里举一个例子来说明如何在无需暂停整个应用程序的情况下获得想要的效果(这是你永远不应该做的).

You need to rethink what you are trying to do with your pause. It's a bit hard to say given that we don't have much info about what you try to achieve with this pause, but I'll give an example here to illustrate how you can get the wanted effect without having to pause the entire app (which you should never do).

假设您要下载文件 A,然后等待 5 秒,然后下载文件 B.您可以通过运行以下命令来完成此操作:

Say you want to download file A, then wait 5 sec ,then download file B. You do this by running something like this:

var file = downloadFileA();
new Thread((ThreadStart)delegate
{
    Thread.Sleep(5000);
    downloadFileB();
});

此外,最好使实际的下载调用异步(如果它正在下载您正在执行的操作),只是为了确保您不会影响 GUI.

Also, it's prefferable to make the actual download-calls async (if it is downloading you are doing), just to make sure that you don't feeze the GUI.

或者,您可以像这样在后台线程中进行下载:

Alternativly, you can do both the downloads in the background-thread like this:

new Thread((ThreadStart)delegate
{
    var file = downloadFileA();
    Thread.Sleep(5000);
    downloadFileB();
});

这篇关于线程睡眠在 Windows Phone 7 上运行不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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