【发布时间】:2017-04-22 15:12:26
【问题描述】:
我的 WPF 应用程序使用外部 DLL 的方法(c++,没有 UI,只是逻辑),如下所示:
[DllImport("myExternDll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern int externalMethod(string str);
int SomeWPFMethod()
{
int res;
try
{
res = externalMethod(str);
}
catch(Exception e)
{
LogError(e)
return -1;
}
return res;
}
注意,SomeWPFMethod 与 UI 线程分开调用(如果有的话)。
当 dll 内部出现问题时,我得到了
“System.AccessViolationException”类型的未处理异常 发生了
异常。
为应用设置了未经处理的异常方法,但这没有任何作用:
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
e.Handled = false;
return;
}
ShowUnhandledException(e);
}
是否有可能以某种方式处理异常以防止应用程序崩溃?
如果外部方法失败,我不想做任何事情,但应用程序应该仍然可以工作。现在它崩溃了。
【问题讨论】:
-
>>Thomas,不,这适用于 WinForms,不适用于 WPF