Application.Current.Shutdown() 不会杀死我的应用程序 [英] Application.Current.Shutdown() is not killing my application
问题描述
我刚刚启动了一个新的 C#/WPF 应用程序,并且正在使用 WPF Contrib 项目 中的 NotifyIcon.我可以启动程序,将退出"菜单项添加到 NotifyIcon 的 ContextMenu,并将该项链接到一个只运行 Application.Current.Shutdown() 的方法.
这会关闭主窗口和 NotifyIcon,但有些东西继续运行 - 从 VS 运行,它不会离开调试模式.什么还在运行?或者我如何检查?
编辑
我刚刚尝试添加一个调用 Application.Current.Shutdown() 的按钮,该按钮可以正确退出.只有当我从 NotifyIcon 调用时才会出现问题.为什么会这样?
为了澄清,我有以下 XAML:
<网格><av:NotifyIcon Icon="/icon/path"文本="虚拟机管理器"名称="notifyIcon"><FrameworkElement.ContextMenu><上下文菜单><MenuItem Header="Exit" Click="MenuItemExit_Click"/></ContextMenu></FrameworkElement.ContextMenu></av:NotifyIcon><Button Content="Button" Click="button1_Click"/></网格>
button1_Click 和 MenuItemExit_Click 是相同的,但前者成功退出应用程序,后者没有.
进一步的实验:即使我将 Application.Current.Shutdown() 移动到另一个方法中并改为调用它,添加一个间接层,按钮仍然有效而图标没有.
找到解决方案了吗?
刚刚找到这个话题,他的解决方案在这里确实有效.我不完全明白发生了什么,所以如果有人愿意解释,我将不胜感激.
你可以试试 Environment.Exit(0);
它会用给定的退出代码杀死进程.退出代码 0 表示应用程序成功终止.它可能更粗鲁"或未完成",但也许这就是您要找的.p>
I've just started a new C#/WPF application and am using the NotifyIcon from the WPF Contrib project. I can start the program, add an "Exit" MenuItem to the NotifyIcon's ContextMenu, and link that item to a method that simply runs Application.Current.Shutdown().
This closes the main window and the NotifyIcon, but something continues to run - running from VS, it does not leave debug mode. What is still running? Or how can I check?
EDIT
I've just tried adding a button that calls Application.Current.Shutdown(), and that exits properly. It's only when called from the NotifyIcon that I have a problem. Why would this be?
To clarify, I have the following XAML:
<Window x:Class="VirtualBoxManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:av="http://schemas.codeplex.com/wpfcontrib/xaml/presentation"
Title="VirtualBox Manager" Height="350" Width="525"
ShowInTaskbar="False" WindowStyle="None">
<Grid>
<av:NotifyIcon Icon="/icon/path"
Text="Virtual Machine Manager"
Name="notifyIcon">
<FrameworkElement.ContextMenu>
<ContextMenu>
<MenuItem Header="Exit" Click="MenuItemExit_Click" />
</ContextMenu>
</FrameworkElement.ContextMenu>
</av:NotifyIcon>
<Button Content="Button" Click="button1_Click" />
</Grid>
Both button1_Click and MenuItemExit_Click are identical, but the former successfully exits the app and the latter does not.
Further experimentation: even if I move Application.Current.Shutdown() into another method and call that instead, adding a layer of indirection, still the button works and the icon doesn't.
Solution found?
Just found this thread, who's solution does work here. I don't totally understand what's happening, so if anybody cares to explain I'd appreciate it.
You coult try Environment.Exit(0);
It kills the process with the given exit code. Exit code 0 states the application terminated successfully. It might be more 'rude' or 'not-done' but perhaps this is what you are looking for.
这篇关于Application.Current.Shutdown() 不会杀死我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!