不叫地铁/ WinRT的UI异步无效事件处理未处理的异常处理程序 [英] Unhandled exception handler not called for Metro / WinRT UI async void event handler

查看:177
本文介绍了不叫地铁/ WinRT的UI异步无效事件处理未处理的异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下是从Windows 8地铁/ WinRT的应用程序,它已经沦落到以显示异常所需的最低限度的提取物:

Consider the following to be extracts from a Windows 8 Metro / WinRT app, which have been reduced to the bare minimum required to show the anomaly:

public class App : Application
{
    public App()
    {
        UnhandledException += (sender, e) => e.Handled = true;
    }
}

public class MainPage : Page
{
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        throw new NotSupportedException();
    }

    private async void Button_Click_2(object sender, RoutedEventArgs e)
    {
        throw new NotSupportedException();
    }
}

所以,给出的Metro UI有两个按钮和他们的点击事件处理程序,唯一不同的是,第二个事件处理程序被标记为异步

然后点击每个按钮,我期望UnhandledException处理程序中的两个的情况下被调用,因为他们(应该)都可以通过UI线程进入和相关的同步上下文。
我的理解是,对于异步无效方法,任何异常应该被捕获并通过初始同步的背景下,这也是'重新抛出'(preserving原来的堆栈跟踪)在异步/等待FAQ 的明确规定。

Then clicking on each button, I would expect the UnhandledException handler to be called in both cases, since they (should) both be entered via the UI thread and associated synchronization context. My understanding is that, for async void methods, any exceptions should be captured and 'rethrown' (preserving the original stacktrace) via the initial synchronization context, which is also clearly stated in the Async / Await FAQ.

但UnhandledException处理程序的不可以堪称异步的情况下,这样的应用程序崩溃!由于这种挑战我认为一个原本非常直观的模型,我需要知道为什么!是的,我知道我可以在包处理程序的身体尝试{}赶上{} ,但我的问题是,为什么不叫逆止UnhandledException处理?

But the UnhandledException handler is not called in the async case, so the application crashes! Since this challenges what I consider an otherwise very intuitive model, I need to know why! Yes, I know I could wrap the body of the handler in a try { } catch { }, but my question is why isn't the backstop UnhandledException handler called?

要进一步强调为什么这没有意义,也使用异步/等待和定位考虑一个WPF应用程序下面几乎相同提取物的.NET Framework 4.5:

To further emphasise why this doesn't make sense, consider the following practically identical extracts from a WPF app also using async / await and targeting .NET Framework 4.5:

public class App : Application
{
    public App()
    {
        DispatcherUnhandledException += (sender, e) => e.Handled = true;
    }
}

public class MainWindow : Window
{
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        throw new NotSupportedException();
    }

    private async void Button_Click_2(object sender, RoutedEventArgs e)
    {
        throw new NotSupportedException();
    }
}

[有是WPF既有的应用程序DispatcherUnhandledException事件处理程序,以及一个AppDomain UnhandledException事件处理一个微妙的差异,但在DispatcherUnhandledException,它与地铁/ WinRT的应用程序保持一致为'处理'你只能标记异常上述UnhandledException事件处理程序。]

[There is a subtle difference that WPF has both an Application DispatcherUnhandledException event handler as well as an AppDomain UnhandledException event handler, but you can only mark the exception as 'handled' in the DispatcherUnhandledException, which aligns with the Metro / WinRT Application UnhandledException event handler above.]

然后点击每个按钮时,DispatcherUnhandledException处理器确实叫两个的情况下,如预期,并且应用程序的不可以崩溃。

Then clicking on each button, the DispatcherUnhandledException handler is indeed called in both cases, as expected, and the application does not crash.

推荐答案

在以下职位的解决方案为我工作,有一个小的变化:我不得不搬到AsyncSynchronizationContext.Register();到App.OnLaunched事件

The solution in the following post worked for me, with one small change: I had to move AsyncSynchronizationContext.Register(); to the App.OnLaunched event

<一个href=\"http://www.markermetro.com/2013/01/technical/handling-unhandled-exceptions-with-asyncawait-on-windows-8-and-windows-phone-8/\">http://www.markermetro.com/2013/01/technical/handling-unhandled-exceptions-with-asyncawait-on-windows-8-and-windows-phone-8/

这篇关于不叫地铁/ WinRT的UI异步无效事件处理未处理的异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆