WPF closes the application method, while wpf closes the application

Source: Internet
Author: User

WPF closes the application method, while wpf closes the application
 

Many people think that closing an Application should be very simple, for example, an Application in Windows form. exit (); method can solve the problem, but do not abuse it in WPF, because the Application class in WPF does not have this method, but there is an Exit event driver, closing a program in a WPF application requires a lot of attention:

When a WPF application is closed, the ShutdownMode attribute is set and the value of the enumerated type in 3 is:

1) Close the application when the last form of the OnLastWindowClose application is closed.

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

3) OnExplicitShutdown: indicates that the call is closed.

In OnExplicitShutdown mode, the ShutDown method of the Application instance must be displayed.

Example: Application. Current. Shutdown (-1); here Application. Current returns the Current Application instance of the Current Application.

Note that the above is not applicable to XBAP, and XBAP is automatically disabled when the browser is closed.

 

 

Requirement: the entire program (WPF) needs to be closed in many subforms)

Winform implementation: Application. Exit ();

WPF implementation:

App. xaml file:

Code <Application x: Class = "pc. App"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri = "Windows1.xaml" ShutdownMode = "OnExplicitShutdown">


Windows1.xaml file (Part ):

<Button Margin = "37,0, 15,15" Style = "{DynamicResource btn_Exit}" Content = "Button" Grid. column = "2" Grid. row = "3" Height = "41" verticalignment = "Bottom" Width = "100" x: Name = "btn_Exits" Click = "btn_Exits_Click"/>


Windows1.xaml. cs file (Part ):

Private void btn_Exits_Click (object sender, RoutedEventArgs e)
{
Application. Current. Shutdown ();

}

 

Very simple! After that, you only need to add Application. Current. Shutdown () to the button event to exit the program.

 

In addition, there is a way to forcibly terminate the application process

 

Environment. Exit (0) can immediately interrupt program execution and Exit

 

This method is similar to finding and terminating a process in the task manager, that is, closing the process immediately, no matter whether the process is currently working or not.

In WPF, this method is called to exit immediately without waiting for the current job to complete.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.