如何捕获由外部dll静默关闭应用程序触发的未处理异常?

时间:2019-03-21 09:38:50

标签: c# wpf

我正在开发使用WPF的光学字符识别软件。 我有第三方硬件可以执行此操作。我也有制造商提供的设备驱动程序。 我在项目中引用了设备驱动程序的dll,并调用了其中一种将图像转换为文本的方法。例如,我可以将护照放在设备上,然后从护照中获取一系列信息。 该功能可以按预期工作,但有时应用程序会关闭而不会引发任何异常。这是由于从窗口事件日志中看到的设备驱动程序dll。 dll中有未处理的异常,突然关闭了应用程序。

我希望我的WPF应用程序能够运行,无论dll文件引发什么异常。

我已在app.cs文件中全局处理错误

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    var dialogContent = "Exception Message:" + "\n" + e.Exception.Message +
                        "\n" + "Stack Trace:" + "\n" + e.Exception.StackTrace;
    MessageBox.Show(dialogContent);

    e.Handled = true;
}

我还添加了尝试在我的方法上添加这些属性的方法。

[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
public void ConvertImagetoText()
{
    try
    {
         //do something from the dll file.
    }
    catch
    {
         throw;
    }
}

0 个答案:

没有答案
相关问题