关于WPF全局异常捕获的问题

三五月儿 2015-10-13 05:17:55
WPF程序中出现下图所示错误:

使用wpf捕获全局异常的方式不活不到吗?

//处理未捕获的异常
//Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
...全文
688 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
三五月儿 2015-10-14
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
试试这个SetUnhandledExceptionFilter 如果还不行,就要写个c的库来调用 public delegate int UnhandledExceptionCallBack(ref long a); [DllImport("kernel32")] private static extern int SetUnhandledExceptionFilter(UnhandledExceptionCallBack f);
我只做了一个下面的实例程序,虽说进入了方法MyExceptionfilter,但是还会弹出系统的异常报告,为什么呢。 using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace CrashExp { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public delegate int CallBack(ref long a); CallBack mycall; [DllImport("kernel32")] private static extern Int32 SetUnhandledExceptionFilter(CallBack cb); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, ref string strBuffer, int nSize); public MainWindow() { InitializeComponent(); mycall = new CallBack(MyExceptionfilter); SetUnhandledExceptionFilter(mycall); } int MyExceptionfilter(ref long a) { MessageBox.Show(a.ToString()); return 0; } private void Button_Click_1(object sender, RoutedEventArgs e) { try { string strText = string.Empty; GetWindowText(new WindowInteropHelper(this).Handle, ref strText, 10);//这里会弹出未捕获到AccessViolationException异常 } catch (AccessViolationException ex) { } } } }
Poopaye 2015-10-13
  • 打赏
  • 举报
回复
试试这个SetUnhandledExceptionFilter
如果还不行,就要写个c的库来调用

public delegate int UnhandledExceptionCallBack(ref long a);
[DllImport("kernel32")]
private static extern int SetUnhandledExceptionFilter(UnhandledExceptionCallBack f);
三五月儿 2015-10-13
  • 打赏
  • 举报
回复
引用 1 楼 shingoscar 的回复:
这错误已经超出托管代码的范围了
那这种异常,该怎么处理呢
Poopaye 2015-10-13
  • 打赏
  • 举报
回复
这错误已经超出托管代码的范围了

110,585

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧