// Any control that causes the Window.Closing even to trigger.
private void MenuItemExit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
// Method to handle the Window.Closing event.
private void Window_Closing(object sender, CancelEventArgs e)
{
var response = MessageBox.Show("Do you really want to exit?", "Exiting...",
MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (response == MessageBoxResult.No)
{
e.Cancel = true;
}
else
{
Application.Current.Shutdown();
}
}
public class CloseAppResult : CancelResult
{
public override void Execute(CoroutineExecutionContext context)
{
Application.Current.Shutdown();
base.Execute(context);
}
}
public class CancelResult : Result
{
public override void Execute(CoroutineExecutionContext context)
{
OnCompleted(this, new ResultCompletionEventArgs { WasCancelled = true });
}
}
如果您正在使用Application.Current.Shutdown()退出应用程序,那么如果您正在从不同的线程调用它,则可能会得到System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.。要解决这个问题,您可以像这样包装调用