Windows 8 系列(六):BackgroundTask 及其引起无法捕获的Crash

今天要给大家分享的是关于BackgroundTask。在此之前,我们先看看这个问题:VS2011 无法捕获的Crash(崩溃)。

在我们某个版本发布了Alpha之后,测试人员就反馈说我们的应用不像前一个版本那么稳定了,总是会自动关闭,或者重新启动,而且操作步骤没有规律,浮现概率低。

我的第一反应是:系统的问题。因为在自测的过程中也有碰到类似问题,比如:在Debug某个功能的时候(通常需要运行大概5分钟或者更长时间),应用程序就会自动关闭,并且vs自动解除Debug状态。这让我很纳闷,如果有异常,应该在Debug状态下抛出给开发者,怎么会无故关闭Debug状态呢??

直到我们临近官方提交应用的截止日期,这个问题被无限放大,成为我们当时最难以解决又不得不解决的问题。没有log、没有异常抛出,我们当时只能尝试着每一条通往答案的路。

 

后来我们发现,当我们只是启动了程序,什么都不做的情况下,依然会Crash,Crash的时间都不定,大概在5-15分钟。我回想了下,当时只有全局的Timer在运行,但是当我们把Timer去掉后,Crash的情况依然存在。

 

于是我们想到了Background,难道程序在运行的时候,Background也在执行??官方文档中描述了

If a background task executes within the app process, the background task infrastructure might need to change the state of the app:

· Running: If the app is running, it is already in the foreground and the background task is simply launched within the app.

· Suspended: If the app is suspended, most of its threads are unfrozen and the background task is launched. For details, see “Threading model for background tasks hosted in the app,” later in this paper. 

· Terminated: If the app is terminated, it is not running at the time of the trigger so the app is launched and the background task is run. The app activation does not involve UI, and it does not bring the app to the foreground.

 

也就是说,在程序运行的时候,BackgroundTask也在运行。但是我们的代码是根据官方提供的Sample中的代码写的,设置也一样,应该不会错啊。经过我们的测试,官方提供的Sample也会出现Crash的情况。。。原因是,我们的开发环境曾经升级了一个内部版本,代码有些变动,但是官方文档中并没有提及相关的变动和相关的方法,这在一定程度上影响了我们的正常开发进度。(但是在此还是要感谢一下微软中国对我们的支持,帮我们向总部反应情况)

 

如下图,由于我们的BackgroundTask是时间触发的,每15分钟触发一次,因此我们在配置文件的Declarations选项卡中添加BackgroundTasks,然后再右侧的设置中只勾选Timer,切Executable项的内容需要为空(官方Sample中勾选了多个,并且Executable项中的内容为:backgroundTaskHost.exe) 

 

然后再试试,Crash现象没有了,BackgroundTask也能正常运行了。我们的努力没有白费啊。希望能够帮助到碰到相关问题的开发者们。

posted @ 2012-05-28 11:58  lihaiyin  阅读(2420)  评论(13编辑  收藏  举报