简体   繁体   English

为什么我的C#WPF程序在Application.Shutdown()之后继续执行行?

[英]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.

相关问题 C# WPF 为什么我的程序在其他计算机上崩溃? - C# WPF Why Does my program crash on other computers? Application.Shutdown() 从 ContextMenu 失败 - Application.Shutdown() fails from ContextMenu 如何判断是通过关闭按钮还是通过WPF中的Application.Shutdown关闭? - How to tell whether closing via close button or via Application.Shutdown in WPF? 为什么我的应用程序在注销/关闭(c#/。net winforms)时无法关闭? - Why does my application not close on logoff/shutdown (c#/.net winforms)? 调用Application.Shutdown()后,使用ShowDialog显示窗口不起作用 - After Application.Shutdown() is called, showing a window using ShowDialog doesn't work C#TCPListener仅在应用程序关闭后才保持第一次监听 - C# TCPListener keep listening after application shutdown only for the first time Environment.Exit() 和 Application.Shutdown() 有什么区别? - what's difference between Environment.Exit() and Application.Shutdown()? 为什么在PowerShell中通过C#应用程序执行“ cmd”命令会冻结它? - Why does executing “cmd” command in PowerShell via my C# application freezes it? 为什么我的C#事件在没有运行应用程序的情况下在XAML(WPF)上触发? - Why does my C# event fires on XAML (WPF) without running the application? 为什么我的带有计时器的C#Winforms程序一天后陷入困境? - Why does my C# Winforms program with a timer get bogged down after a day?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM