c# – 如果注册了DispatcherUnhandledException,则在Control.LayoutUpdated中发生异常后堆栈溢出
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如果注册了DispatcherUnhandledException,则在Control.LayoutUpdated中发生异常后堆栈溢出,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4156字,纯文字阅读大概需要6分钟。
内容图文
最近,我遇到了一个问题,我仍然不知所措.在应用程序中,我注册了一个调度程序异常处理程序.在同一个应用程序中,第三方组件(DevExpress网格控件)在Control.LayoutUpdated的事件处理程序中导致异常.我希望,调度程序异常处理程序被触发一次.但相反,我得到了堆栈溢出.我生成了一个没有第三方组件的示例,发现它发生在每个WPF应用程序中.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
namespace MyApplication
{
/* App.xaml
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyApplication.App"
Startup="OnStartup"
/>
*/
public partial class App
{
private void OnStartup(object sender, StartupEventArgs e)
{
DispatcherUnhandledException += OnDispatcherUnhandledException;
MainWindow = new MainWindow();
MainWindow.Show();
}
private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message);
e.Handled = true;
}
}
public class MainWindow : Window
{
private readonly Control mControl;
public MainWindow()
{
var grid = new Grid();
var button = new Button();
button.Content = "Crash!";
button.HorizontalAlignment = HorizontalAlignment.Center;
button.VerticalAlignment = VerticalAlignment.Center;
button.Click += OnButtonClick;
mControl = new Control();
grid.Children.Add(mControl);
grid.Children.Add(button);
Content = grid;
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
mControl.LayoutUpdated += ThrowException;
mControl.UpdateLayout();
mControl.LayoutUpdated -= ThrowException;
}
private void ThrowException(object sender, EventArgs e)
{
throw new NotSupportedException();
}
}
}
有什么方法可以防止这种行为吗?它发生在.NET framework 3.0,3.5,4.0和4.5中.我不能只围绕LayoutUpdated事件处理程序包装一个try-catch,因为它在第三方组件中,我不认为,堆栈溢出应该发生.
解决方法:
我认为Florian GI对于消息框是正确的,但是如果在OnDispatcherUnhandledException方法中它没有做其他事情(或者只是设置Handled为true)而不是消息框,它仍然永远循环并且没有到达mControl.LayoutUpdated – = ThrowException;线.
所以我想我会用dotPeek对代码进行一些调整…
当你在控件上调用UpdateLayout时,最终它会到达方法ContextLayoutManager.UpdateLayout,这个方法的片段如下所示:
// ... some code I snipped
bool flag2 = true;
UIElement element = (UIElement) null;
try
{
this.invalidateTreeIfRecovering();
while (this.hasDirtiness || this._firePostLayoutEvents)
{
//... Loads of code that I think will make sure
// hasDirtiness is false (since there is no reason
// for anything remaining dirty - also the event is
// raised so I think this is a safe assumption
if (!this.hasDirtiness)
{
this.fireLayoutUpdateEvent();
if (!this.hasDirtiness)
{
this.fireAutomationEvents();
if (!this.hasDirtiness)
this.fireSizeChangedEvents();
}
}
//... a bit more
flag2 = false;
}
}
finally
{
this._isUpdating = false;
this._layoutRequestPosted = false;
//... some more code
if (flag2)
{
//... some code that I can't be bothered to grok
this.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (Delegate) ContextLayoutManager._updateLayoutBackground, (object) this);
}
}
// ... and for good measure a smidge more code
我要采取一个平底船,并建议你的情况下_firePostLayoutEvents标志是真的.
其中_firePostLayoutEvents设置为false的唯一地方是在fireAutomationEvents方法,以便让我们假设fireAutomationEvents方法你会抛出异常(我猜的fireLayoutUpdateEvent方法),因此这个标志将不会设置为false结束前的某个地方.
但是,当然,finally终止于循环之外,所以它不会永远循环(如果它没有得到StackOverflowException).
是的,向前,所以我们正在调用UpdateLayoutBackground函数,它实际上只是调用NeedsRecalc所以让我们来看看……
private void NeedsRecalc()
{
if (this._layoutRequestPosted || this._isUpdating)
return;
MediaContext.From(this.Dispatcher).BeginInvokeOnRender(ContextLayoutManager._updateCallback, (object) this);
this._layoutRequestPosted = true;
}
Rightyho,就是调用UpdateLayoutCallback所以眯着眼睛……
private static object UpdateLayoutCallback(object arg)
{
ContextLayoutManager contextLayoutManager = arg as ContextLayoutManager;
if (contextLayoutManager != null)
contextLayoutManager.UpdateLayout();
return (object) null;
}
Oooooh,这很有趣 – 它再次调用UpdateLayout – 因此,我会默默地猜测这是你问题的根本原因.
因此,我不认为你能做些什么我害怕.
内容总结
以上是互联网集市为您收集整理的c# – 如果注册了DispatcherUnhandledException,则在Control.LayoutUpdated中发生异常后堆栈溢出全部内容,希望文章能够帮你解决c# – 如果注册了DispatcherUnhandledException,则在Control.LayoutUpdated中发生异常后堆栈溢出所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。