0

的文档BackgroundTaskCompletedEventArgs.CheckResult说:

如果后台任务完成事件报告错误,则抛出异常。

我尝试制作一个在其Run方法中引发异常的后台任务,但是当我订阅它的Completed事件并CheckResult在它完成时调用时,不会引发异常。

什么时候CheckResult真正抛出异常?

4

1 回答 1

1

It should work exactly as you have described: if an exception is thrown in IBackgroundTask's Run method, then BackgroundTaskCompletedArguments.CheckResult() method throws an exception when you call it.

Make sure that your background task actually runs and that your app is in foreground otherwise IBackgroundTaskRegistration.Completed event is not raised at all.

If you want to try it out on a working exaple, download Background task sample and make two modifications to it:

  • In Task\ServicingComplete.cs add throw new Exception(); at the very end of Run method.
  • In BackgroundTask\ServicingCompleteTask.xaml.cs add args.CheckResult(); at the beginning of OnCompleted method.

If you now run the app, register the task from the UI and trigger it from Visual Studio's Debug Location toolbar when the app is in foreground, you will notice that the CheckResult() call will throw an exception as expected.

于 2013-03-23T08:30:47.713 回答