即使在显式关闭后,WPF应用程序仍将继续
我有一个简单的wpf应用程序,即使在我显式地调用它关闭后,它仍会继续运行 它与第三方应用程序集成,需要在初始化时检查某些类型和特定内容的文档是否打开 以下是初始化代码的一部分:即使在显式关闭后,WPF应用程序仍将继续,wpf,Wpf,我有一个简单的wpf应用程序,即使在我显式地调用它关闭后,它仍会继续运行 它与第三方应用程序集成,需要在初始化时检查某些类型和特定内容的文档是否打开 以下是初始化代码的一部分: try { ActiveProductDoc = Automation.CATIA.ActiveDocument as ProductDocument; } catch { InvalidAssemblyShutdown("You must have an assembly open before yo
try
{
ActiveProductDoc = Automation.CATIA.ActiveDocument as ProductDocument;
}
catch
{
InvalidAssemblyShutdown("You must have an assembly open before you run the app");
}
if(ActiveProduct == null)
InvalidAssemblyShutdown("You must have one assembly open (not a part)");
ActiveProduct = ActiveProductDoc.Product;
以下是残障人士的组装和关闭方法:
private void InvalidAssemblyShutdown(string message)
{
MessageBox.Show(message);
Close();
Application.Current.Shutdown();
}
我已将应用程序的ShutdownMode属性设置为OnMainWindowClose
我目前正在做一个用例测试,用户打开的文档类型错误,因此ActiveProduct字段为空。InvalidAssemblyShutdown方法按预期调用,但尽管如此,shutdown调用之后的初始化方法中的行仍然运行并抛出异常
知道发生了什么事吗
我应该抛出异常并使用全局异常处理程序吗 如果查看
Application.Current.Shutdown
()的源代码,您将看到它使用Dispatcher.BeginInvoke()
来启动关机。换句话说,关闭在UI线程上排队。它不会在精确的方法调用期间生效,因此下面的代码将继续执行
如果不希望在处理关闭请求时运行某些代码,则需要在调用Application.Current.Shutdown
后立即退出代码。比如:
if(ActiveProduct == null)
{
InvalidAssemblyShutdown("You must have one assembly open (not a part)");
return; // prevent further code execution.
}
值得一提的是,
this.Close()
的工作方式与此类似。所以,如果您有适当的流控制,就根本不需要调用Application.Current.Shutdown
。您对this.Close()
的调用应该足够了。如果您查看了Application.Current.Shutdown
()的源代码,您将看到它使用Dispatcher.BeginInvoke()
来启动关机。换句话说,关闭在UI线程上排队。它不会在精确的方法调用期间生效,因此下面的代码将继续执行
如果不希望在处理关闭请求时运行某些代码,则需要在调用Application.Current.Shutdown
后立即退出代码。比如:
if(ActiveProduct == null)
{
InvalidAssemblyShutdown("You must have one assembly open (not a part)");
return; // prevent further code execution.
}
值得一提的是,
this.Close()
的工作方式与此类似。所以,如果您有适当的流控制,就根本不需要调用Application.Current.Shutdown
。你调用this.Close()
应该足够了。这个答案太简单了。如果你愿意,我相信我们可以想出更复杂的答案。这个答案太简单了。如果你愿意,我相信我们可以想出更复杂的答案。