This WPF program displays a ContextMenu hosting a MenuItem labeled 'Exit', along with an empty Window. Selecting 'Exit' should terminate the process, but it only closes the Window and ContextMenu. I am not looking to forcibly terminate this program, but end it cleanly.
Why does calling Application.Shutdown() in the Click event handler fail to shut down the program?
using System;
using System.Windows;
using System.Windows.Controls;
class MyApp : Application {
[STAThread]
public static void Main() {
new MyApp().Run();
}
protected override void OnStartup(StartupEventArgs e) {
new Window().Show();
MenuItem menuItem = new MenuItem();
menuItem.Header = "Exit";
menuItem.Click += delegate { Shutdown(); };
ContextMenu contextMenu = new ContextMenu();
contextMenu.Items.Add(menuItem);
contextMenu.IsOpen = true;
}
}