C# AppDomain.UnhandledException应在哪里订阅?
建议的副本是关于C# AppDomain.UnhandledException应在哪里订阅?,c#,.net,wpf,C#,.net,Wpf,建议的副本是关于DispatcherUnhandledException,而不是AppDomain.CurrentDomain.UnhandledException 原件: 应在哪里订阅AppDomain.UnhandledException?上的示例仅在Main中显示它,这就是我在Winforms中所做的:我在程序中订阅它。Main: AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
DispatcherUnhandledException
,而不是AppDomain.CurrentDomain.UnhandledException
原件:
应在哪里订阅AppDomain.UnhandledException
?上的示例仅在Main
中显示它,这就是我在Winforms中所做的:我在程序中订阅它。Main:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
但是我在WPF中找不到一个合适的位置,在那里
Main
是隐藏的。我已经搜索过了,但主要是找到了是否订阅的讨论,假设读者现在会在哪里。假设您在Visual Studio中使用默认模板创建了项目,您应该有一个名为app.xaml
的文件,下面还有一个名为app.xaml.cs
的文件
在应用程序
类中,您可以将其添加到启动时的开始处
(以及调度程序。未处理的异常
):
实际上,
Dispatcher.UnhandledException
在大多数情况下应该足够了。这意味着您可以完全跳过使用AppDomain.CurrentDomain.UnhandledException。同时注册到AppDomain.CurrentDomain.UnhandledException
的唯一原因是在主UI线程以外的线程中引发异常。但是我认为最好是在各自的线程中捕获这些异常。假设您在Visual Studio中使用默认模板创建了项目,那么您应该有一个名为app.xaml
的文件,在该文件下还有一个名为app.xaml.cs
的文件
在应用程序
类中,您可以将其添加到启动时的开始处
(以及调度程序。未处理的异常
):
实际上,
Dispatcher.UnhandledException
在大多数情况下应该足够了。这意味着您可以完全跳过使用AppDomain.CurrentDomain.UnhandledException。同时注册到AppDomain.CurrentDomain.UnhandledException
的唯一原因是在主UI线程以外的线程中引发异常。但是我认为在各自的线程中捕捉这些异常是更好的做法。您可以覆盖App.xaml.cs
中的onstart
方法,这是最接近Main
方法的方法(如果愿意,也可以使用App
构造函数`)
请注意,这要求您省略
StartupUri
并自己打开一个窗口。您可以覆盖App.xaml.cs
中的OnStartup
方法,该方法是最接近Main
方法的方法(或者使用App
构造函数,如果愿意的话)
请注意,这要求您省略StartupUri
并自己打开窗口。WPF中的Main()入口点是从App.xaml源文件自动生成的。订阅事件的最早机会是在App.xaml.cs中的App类的构造函数中:
public partial class App : Application {
public App() {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc...
}
}
但这并不是“完美的”,您无法在应用程序类的字段初始值设定项中检测到任何故障。与应用程序类中使用的任何类型的JIT编译失败一样,缺少程序集或版本控制问题是常见的问题制造者
为了避免遗漏这些,您需要放弃自动生成的代码,编写自己的Main()方法。假设您没有大量修改app.xaml文件。从项目中删除该文件并添加一个新类,我建议Program.cs:)并使其看起来类似于以下内容:
using System;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1 {
class Program {
[STAThread]
public static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Start();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Start() {
var app = new App();
app.Run(new MainWindow());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc..
}
}
public class App : Application {
// etc..
}
}
WPF中的Main()入口点是从App.xaml源文件自动生成的。订阅事件的最早机会是在App.xaml.cs中的App类的构造函数中:
public partial class App : Application {
public App() {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc...
}
}
但这并不是“完美的”,您无法在应用程序类的字段初始值设定项中检测到任何故障。与应用程序类中使用的任何类型的JIT编译失败一样,缺少程序集或版本控制问题是常见的问题制造者
为了避免遗漏这些,您需要放弃自动生成的代码,编写自己的Main()方法。假设您没有大量修改app.xaml文件。从项目中删除该文件并添加一个新类,我建议Program.cs:)并使其看起来类似于以下内容:
using System;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1 {
class Program {
[STAThread]
public static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Start();
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Start() {
var app = new App();
app.Run(new MainWindow());
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
// etc..
}
}
public class App : Application {
// etc..
}
}
您可以在app.xaml.cs中执行此操作。请参阅:[未处理的异常][1][1]:@musium这仅适用于DispatcherUnhandledException
。至少intellisense在AppDomain.UnhandledException
中没有显示相同的内容。这是WPF中最接近UnhandledException(Winforms)的内容。看:@musium不,不是。请参阅。您可以在app.xaml.cs中执行此操作。请参阅:[未处理的异常][1][1]:@musium这仅适用于DispatcherUnhandledException
。至少intellisense在AppDomain.UnhandledException
中没有显示相同的内容。这是WPF中最接近UnhandledException(Winforms)的内容。看:@musium不,不是。看,谢谢。在启动之后,我需要调用base
,还是它通常是空的?是的,你应该调用它:@xanatos:的确。无意中从我复制的示例代码中删除了它。即使它现在什么也没用,我通常也会把它包括进去。谢谢。在启动之后,我需要调用base
,还是它通常是空的?是的,你应该调用它:@xanatos:的确。无意中从我复制的示例代码中删除了它。即使它现在什么也没做,我通常也会把它包括进去。谢谢你,但我也在编辑它。我会换一种方式,没问题。继续:)我更喜欢在调用基本实现之前设置回调。这是一个好的实践吗?我想说调用WPF基代码比您自己的调用更重要。在这种情况下,我不会这样做,或者使用尝试。。。捕获
以确保调用它。Fai