的文档BackgroundTaskCompletedEventArgs.CheckResult
说:
如果后台任务完成事件报告错误,则抛出异常。
我尝试制作一个在其Run
方法中引发异常的后台任务,但是当我订阅它的Completed
事件并CheckResult
在它完成时调用时,不会引发异常。
什么时候CheckResult
真正抛出异常?
的文档BackgroundTaskCompletedEventArgs.CheckResult
说:
如果后台任务完成事件报告错误,则抛出异常。
我尝试制作一个在其Run
方法中引发异常的后台任务,但是当我订阅它的Completed
事件并CheckResult
在它完成时调用时,不会引发异常。
什么时候CheckResult
真正抛出异常?
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:
Task\ServicingComplete.cs
add throw new Exception();
at the very end of Run
method.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.