【发布时间】:2016-06-11 23:30:19
【问题描述】:
我有一个运行系统托盘应用程序的 C# 程序 - 在后台传输/移动/创建/编辑文件。
如果用户手册删除程序正在使用的文件,则有很多异常处理和循环处理以防止程序崩溃/卡住。
不幸的是,其中一台正在运行该程序的计算机发生了程序崩溃。电脑好难弄,不能装调试软件(外边是嵌入式PC,没有网络)。
我已尝试查找崩溃的原因(例如未处理的异常),但没有找到任何东西,也无法访问远程位置的 PC。
我希望找到一种方法来使用 AppDomain / UnhandledExceptionEventHandler 来捕获所有未处理的异常并将它们记录下来以进行诊断。 但是,我(故意)在“DoSomething.class”类中的办公室中创建的异常正在使 WinForm 应用程序崩溃,而不是记录异常错误。
我的代码是:
//Current domain for the unhandled exception handling
AppDomain currentDomain;
[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
public MainForm()
{
InitializeComponent();
currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
CreateTimer.Interval = Settings.Default.CreateTimer;
CreateTimer.Start();
RunCopyProtocol();
ChangeFilesTimer.Interval = 10000;
ChangeFilesTimer.Start();
RunChangeFiles();
throw new Exception("ABC");
}
public void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
{
try
{
Exception ex = (Exception)args.ExceptionObject;
SetLabel(ex);
}
finally { }
}
private string SetLabel(Exception ex)
{
String str;
str = ex.Message;
Label1.Text = str;
return str;
}
private void ChangeFilesTimer_Tick(object sender, EventArgs e)
{
RunChangeFiles();
throw new Exception("2");
}
为什么 throw new 异常不调用未处理异常错误? 我将如何使用 AppDomain 获取未处理的异常处理程序来处理 RunChangeFiles 中的这个异常/异常?
AppDomain 上的代码基于MSDN examples
【问题讨论】:
-
您可能对 AppDomain.UnhandledException 将允许您做什么有一个错误的想法。它不会阻止应用程序在发生未处理的异常时结束。它只是一个通知,允许您在进程结束之前进行清理或记录。
标签: c# winforms exception exception-handling appdomain