[英]Why does my C# WPF program keep executing lines after Application.Shutdown()?
Here's a snippet of code where I pop up a simple dialog ("chooser"). 这是一段代码,我弹出一个简单的对话框(“选择器”)。 Depending on the user's input, the application might terminate. 根据用户的输入,应用程序可能会终止。
DPChooser chooser = new DPChooser(dataProvider);
if (chooser.ShowDialog() == false)
Application.Current.Shutdown(0);
else
ApplicationContext.Current.InitializeDataProviderAPI(chooser.DataProvider);
}
// more code continues here
// THE PROBLEM:
// Even when Shutdown() above is called,
// the execution continues proceeding past here!
I've run it in a debugger, so I know that the if is evaluating to false, and I know that Shutdown() is being called. 我在调试器中运行它,所以我知道 if正在评估为false,我知道正在调用Shutdown()。
So why doesn't it shut down? 那为什么不关闭呢?
Note: it's not a threading thing, I think. 注意:我认为这不是一个穿线的东西。 I'm not yet starting anything on other threads. 我还没有在其他线程上开始任何事情。 Even if threading was involved, I'd still not expect the code in this thread to keep proceeding past Shutdown(). 即使涉及到线程,我仍然不希望这个线程中的代码继续经过Shutdown()。
Shutdown stops the Dispatcher processing, and closes the application as far as WPF is concerned, but doesn't actually kill the current thread. Shutdown停止Dispatcher处理,并且就WPF而言关闭应用程序,但实际上并不会终止当前线程。
In your case, you need to prevent code beyond that call from running. 在您的情况下,您需要阻止该调用之外的代码运行。 A simple return will suffice: 一个简单的回报就足够了:
if (chooser.ShowDialog() == false)
{
Application.Current.Shutdown(0);
return;
}
else { //...
It doesn't Terminate your process instantly, its a controlled shutdown. 它不会立即终止您的过程,它是一个受控的关闭。
if you want ( not recommended ) to kill instantly 如果你想(不推荐)立即杀死
Process.GetCurrentProcess().Kill();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.