- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想让我的程序休眠几秒钟。当我使用 thread.sleep(x)
时,整个程序没有响应。根据这篇文章 -> http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx不负责3秒的应用不通过认证:/(我要等5秒)。
最佳答案
不要让应用程序的主线程休眠。相反,做任何需要在应用程序的后台线程中等待的事情。
如果您在应用程序的主线程中调用 Thread.Sleep(),应用程序将无法响应幕后的 UI 消息循环,如您所见。这是因为您在处理一条消息(触发等待的 UI 事件)时阻止了执行,因此无法处理该应用的其他消息。
相反,构建此代码以异步运行。我猜想有一些方法包含大部分繁重的工作,包括 sleep 。只要它不是事件处理程序(如果是,就重构它),你可以设置第二个线程来运行这个方法,并添加一个“回调”方法,该方法将在它完成时调用,这将更新 UI结果。这需要多做一些工作,但它可以保持应用程序的响应速度,而且这通常是一种很好的做法,尤其是在多核设备上(现在甚至手机都配备了双核 CPU)。有许多方法可以设置多线程操作。 Delegate.BeginInvoke 是我的最爱:
public delegate void BackgroundMethod()
public void HandleUITrigger(object sender, EventArgs e)
{
//we'll assume the user does something to trigger this wait.
//set up a BackgroundMethod delegate to do our time-intensive task
BackgroundMethod method = DoHeavyLifting;
//The Delegate.BeginInvoke method schedules the delegate to run on
//a thread from the CLR's ThreadPool, and handles the callback.
method.BeginInvoke(HeavyLiftingFinished, null);
//The previous method doesn't block the thread; the main thread will move on
//to the next message from the OS, keeping the app responsive.
}
public void DoHeavyLifting()
{
//Do something incredibly time-intensive
Thread.Sleep(5000);
//Notice we don't have to know that we're being run in another thread,
//EXCEPT this method cannot update the UI directly; to update the UI
//we must call Control.Invoke() or call a method that Invokes itself
ThreadSafeUIUpdate();
}
public void ThreadSafeUIUpdate()
{
//A simple, thread-safe way to make sure that "cross-threading" the UI
//does not occur. The method re-invokes itself on the main thread if necessary
if(InvokeRequired)
{
this.Invoke((MethodInvoker)ThreadSafeUIUpdate);
return;
}
//Do all your UI updating here.
}
public void HeavyLiftingFinished()
{
//This method is called on the main thread when the thread that ran
//DoHeavyLifting is finished. You can call EndInvoke here to get
//any return values, and/or clean up afterward.
}
关于c# - 线程 sleep 在 Windows Phone 7 上运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812435/
我开发了一个使用 mspn 服务的 windows phone 应用程序。它在 Windows 手机模拟器上运行良好。现在我想在 Windows Phone 设备中测试它。我有一个 Windows P
我正在考虑针对 Windows Phone 7 进行开发。为了测试我的应用程序,我需要能够模拟电话调用。在模拟器中可以吗? 最佳答案 不,不可能模拟您的应用程序被接收到的电话中断。还宣布为 the R
我看到 Windows Phone 8 支持 Windows Phone 7 应用程序。我在 Windows Phone 7 上开发了一些应用程序,我想在 Windows Phone 8 模拟器上运行
我正在Windows 8上使用Visual Studio2013。我有三个Windows Phone设备,lumia 620、920、1320,这些都是开发人员解锁的设备。 620设备已安装Windo
非常直接的问题。我的公司开发了一个 windows phone 7 应用程序,并一直在 windows phone 7 设备上对其进行测试。我们能否安全地假设同一个应用程序将向后兼容并在 Window
标题几乎概括了它。我已经编写了一个 Windows Phone 7 应用程序,现在我想将它部署到 Windows Phone 8 设备 (HTC Windows Phone 8X)。我已经注册并解锁了
我一直在开发 Windows Phone 8 应用程序,现在我想将其更改为 Windows Phone 8.1。我该怎么做? 是否应该创建一个新的 Windows Phone 8.1 应用并将所有现有
我有一个针对 Windows Phone 8 的应用程序。现在我想使用 this compression library其中指出: Supported Platforms: .NET Framewor
有什么区别Windows Phone 7.1 (NoDo) 和 Windows Phone 7.5 (芒果) ? 我已经为 Mango 安装了 SDK,但是当我尝试创建一个项目时,它显示为 7.1 版
我最近更新了我的 Visual Studio 2013,现在它允许开发 Windows Phone 8.1 应用程序。 但是现在,当我创建一个 Windows Phone 项目时,它是针对 WP 8.
我最近购买了 Lumia 630 双 SIM 卡,并一直在尝试注册我的手机以进行开发,但一直收到此错误“无法连接到手机。请确保通过 USB 传输服务的 Windows Phone IP 正在运行”。
我正在尝试使用 documentation from MSDN 在我现有的 Windows Phone OS 7.1 应用程序中支持新的 Windows Phone 磁贴功能。 .但是,我似乎无法通过
我在搜索如何启动Windows Phone 10模拟器时迷路了。 我已经做了: 我下载并安装了仿真器镜像(我知道flash.vhd文件的位置) 我正在运行Windows 8.1 x64专业版。 Hyp
我想制作像图片一样的动态磁贴。我该怎么做? 最佳答案 上面创建的图 block 很可能是在服务器端创建的。据说 Windows Phone 7.1 和 Windows Phone 8 之间发生了很多变
我刚刚将它安装在我的 Windows 8 上。它不是在 VM 上运行,而是在带有 bootcamp 的 macbook 上运行。 当我尝试模拟我的应用程序时,出现第一个错误: 但是后来我单击“重试”并
Microsoft 刚刚发布了结合了 Windows 8.1 和 Windows Phone 8.1 的 Windows App 8.1,因此您可以创建一个通用的应用程序。但是,将 Microsoft
是否有可用于WP7的日历控件(不是日期选择器和时间选择器)? 最佳答案 我还在CodePlex上启动了新项目。我刚刚研究了日历控件的Alpha版本。 http://wpcontrols.codeple
我在XAML中有一个文本框: .cs文件声明了以下事件: private void blah_OnKeyDown(object sender, KeyEventArgs e) {
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在为Windows Phone 7创建一个多人游戏。如何调试模拟器的多个实例以便对其进行调试? 最佳答案 实际上,您可以同时运行Windows Phone 7模拟器的多个实例,甚至可以同时调试它们
我是一名优秀的程序员,十分优秀!