This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19424|Reply: 0

[WPF] WPF closes windows and processes

[Copy link]
Posted on 12/8/2015 1:02:23 PM | | |
Many people think that closing an application should be simple, such as an Application.Exit() in a Windows Form; The method can solve the problem, but don't abuse it in WPF, because the Application class does not have this method in WPF, but there is an Exit event driver, and closing the program in the WPF application pays a lot of attention:

The closure in a WPF application has the ShutdownMode property setting, with the values of the 3 enum types:

1) OnLastWindowClose the application when the last form is closed

2) OnMainWindowClose the application when the main form of the application is closed

3) OnExplicitShutdown shows call closed

In OnExplicitShutdown mode, the ShutDown method that calls the Application instance must be displayed

For example: Application.Current.Shutdown(-1); Here Application.Current returns the current Application instance of the current application.

         Note that the above does not apply to XBAP, which automatically closes when the browser is closed.

  1. <Window x:Class="WpfFistDemo.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="350" Width="525" Loaded="windows_load" Closing="Window_Closing" >
  5.     <Grid>

  6.     </Grid>
  7. </Window>
Copy code
  1. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  2.         {
  3.             System.Environment.Exit(System.Environment.ExitCode);
  4.         }
Copy code
If the window starts a subthread: Application.Current.Shutdown(); Doesn't shut down the application's process, System.Environment.Exit(System.Environment.ExitCode); This will close all the child processes and end the process!





Previous:WPF settings and getting launch parameters
Next:.net implements a simple web server using HttpListener
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com