- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用后台线程对网络服务进行异步调用并返回结果。当后台线程正在运行并且我的应用程序被停用时,假设用户按下了主页按钮,我该如何处理?
private async void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
//What should I do here?
}
我从下面的博客中了解到http://www.wintellect.com/blogs/jgarland/proceed-with-caution---windows-phone-8-app-lifecycle-events-vs-async-methods我不能等待,直到后台线程在停用方法中完成。它还建议使用同步调用。如果我必须在停用事件中取消我的后台线程,我如何找到目前正在运行的后台线程?在每个页面中在调用web服务的地方,使用了后台线程。有没有办法在应用程序再次激活后恢复后台线程?
最佳答案
你是对的,当你的应用程序被停用时,所有进程都会停止 - MSDN source :
When the user navigates forward, away from an app, after the Deactivated event is raised, the operating system will attempt to put the app into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory.
在您的停用事件中,您应该向所有后台线程调用取消请求。如果您使用 aync-await然后你可以用 CancellationTokenSource 装备这些方法.以下是更多相关信息:Enabling Progress and Cancellation in Async , Cancel an Async Task or a List of Tasks , 你可以找到很多关于 Stephen Cleary blog 的信息.
至于知道哪个任务正在运行的问题,我认为您可以使用此 CancellationTokenSource 来检查它 - 如果它不为空,则表示您的后台线程正在运行。
至于重新激活您的方法 - 一切都取决于您的代码和该方法执行的操作。如果您正在下载一组文件,那么您可以记住下载了哪些文件,并在激活调用方法后下载其余文件。如果您执行一些计算,那么您可以记住该方法停止的地方并尝试恢复进一步的计算。认为有时很难。你必须记住你有 limited time to 10 seconds :
You should not wait until the Deactivated event occurs to store this data. Remember that all application lifecycle events enforce a limit of 10 seconds for an application to complete any tasks.
关于其他可能性:
如果你想在后台进程中执行一些操作 - 那么你可以使用 Background agents和 Background transfers如果你想下载一个文件。还有背景音频,但我想您不会问这个问题。
如果您希望您的应用程序在锁屏时保持在前台,那么您可以 Deactivate Idle Detection .
关于c# - 应用程序停用时如何处理后台线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871048/
我找不到在来电时激活和停用振动的方法。 菜单中的选项 --> 设置 --> 声音和显示提到 - PHONE VIBRATE - 来电时手机振动... 我想通过代码激活和停用它(如果可能的话)。 最佳答
我有两个元素在彼此之上。当我点击第一个 div 上的按钮时,第二个 div 在第一个 div 之上打开,我想要做的是让底层 div 成为非交互式的(我不能点击底层 div 上的任何东西只要 overl
有没有办法取消 UIScrollView 的减速? 我想允许用户滚动 Canvas ,但我不希望用户抬起手指后 Canvas 继续滚动。 最佳答案 这可以通过利用 UIScrollView 委托(de
这里是关于 Stack Oveflow 的第一个问题,所以不要作恶! :) 言归正传:如果有堆叠的元素和堆叠的操作区域,如何继续操作以确保您对所看到的内容而不是底层元素进行操作? 我正在学习有关 Qt
这个问题已经有答案了: Deleting Objects in JavaScript (14 个回答) 已关闭 9 年前。 我有一个类,我通过以下方式调用: this.infiniteScroll =
我有一个优化问题,正在尝试使用 optaplanner 来解决。求解算法使用一组规则。引擎使用一个对象来捕获每个规则的权重。规则的最终得分是规则的中间得分乘以权重。分数设置在每条规则的右侧。每个规则的
有没有办法取消 UIScrollView 的减速? 我想允许用户滚动 Canvas ,但我不希望用户抬起手指后 Canvas 继续滚动。 最佳答案 这可以通过利用 UIScrollView 委托(de
我正在尝试更新 native android 应用程序,该应用程序以前是由其他一些人在某些跨平台技术(Titanium)中构建和上传的。应用程序以高级模式发布,其中针对平板电脑和手机有不同的构建。但现
我有一个 JList 列表和以下代码行: list.getInputMap().put(KeyStroke.getKeyStroke('d'), "action"); 因此,当我的列表处于焦点状态并且
有没有办法通过 SQL 语句停用 postgres 用户帐户? 我想阻止用户使用他们的数据库,但不删除用户或他们的数据库。 最佳答案 您还可以考虑 ALTER USER someone WITH NO
我有一个问题。我有一个 ViewController1,它通过 Push-segue 打开 ViewController2。//两者都是NavigationControllers - (void)pr
当我去 Playground 写 let test = "\u{062F}\u{0625} Hello" 时,我得到 Hello دإ(通过当我从输出控制台复制到这里时,我得到 دإ Hello) 似
我想通过扬声器播放歌曲,同时能够使用 Quickblox 接听视频通话。 我的音频速率越来越乱了。还有一个更大的问题是,当通话结束时,quickblox 框架将 Audio Session 设置为停用
我有一个工作项目,我以 tomcat 用户身份登录,但我不知道如何注销,我尝试停用 tomcat session ,我们使用 java spring,这是我尝试从 Controller : @Requ
我正在使用 javascript 来缩放我的 asp.net 网页上的图像。我想在上面放 2 个按钮,例如“缩放”、“取消缩放”,并相应地激活/停用 javascript 功能。现在我有一个 java
我有一个 TextField 和一个按钮。此 TextField 最多可包含 3 个字母或数字。 这是我的问题。当程序运行时,如果这个文本字段为空或者如果这个文本字段不只包含数字,我希望我的按钮被禁用
我有几个组,可以选择三个按钮。我试图做到这一点,以便当有人选择 N/A 按钮时,它会禁用其他两个按钮。当取消选择 N/A 按钮时,将启用其他两个按钮。我让它在我的机器上工作,其他两个按钮被着色为禁用,
HTML: Button Main Menu A Main Menu B
我是 php 新手,如何才能完成这项工作 删除.php prepare("UPDATE tbluser set status=1 WHERE id=:id"); $stmt->execute(
周五,我开始在运行 Ubuntu 14.04 的 VPS 上编写我的第一个 python API hello world 示例。我使用 python3,创建文件夹,virtualenv,激活它,然后断
我是一名优秀的程序员,十分优秀!