Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.Current.Shutdown() is not killing my application

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.

like image 272
tsvallender Avatar asked Feb 21 '11 20:02

tsvallender


2 Answers

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.

like image 159
Maarten Terpstra Avatar answered Nov 05 '22 16:11

Maarten Terpstra


I found if I create threads that are not set to "background", the main window/etc will close, but the threads will keep runnings.

In other words, only background threads close themselves when the main thread ends. Regular threads, aka Thread.IsBackground = false, will keep the process running.

Try using thread.IsBackground = true;

P.S. I made the assumption that you used threads somewhere.

like image 11
Bengie Avatar answered Nov 05 '22 17:11

Bengie