C# Application Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
You can use the Environment.Exit
method to exit a console application or a WinForms/WPF application. Here is an example:
Console.WriteLine("Exiting application..."); Environment.Exit(0);
This will display "Exiting application..." on the console, and then exit the application with a code of 0.
You can handle the Application.Exit
event to perform some actions before the WPF application exits. Here is an example:
private void Application_Exit(object sender, ExitEventArgs e) { // Perform some actions before the application exits }
You can subscribe to the Application.Exit
event in the App.xaml.cs
file of your WPF application:
<Application x:Class="WpfApp1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" Exit="Application_Exit"> <Application.Resources> </Application.Resources> </Application>
This will call the Application_Exit
method before the WPF application exits.
By default, a WPF application doesn't exit when the main window is closed. You can change this behavior by setting the ShutdownMode
property of the Application
class to OnMainWindowClose
. Here is an example:
<Application x:Class="WpfApp1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> </Application.Resources> </Application>
Then in your MainWindow.xaml.cs
file, set the MainWindow
property of the Application
class to the current window:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Application.Current.MainWindow = this; } }
This will exit the WPF application when the main window is closed.
You can use the Application.Restart
method to restart a WinForms application. Here is an example:
Console.WriteLine("Restarting application..."); Application.Restart();
This will display "Restarting application..." on the console, and then restart the WinForms application. Note that this will close all open forms before restarting the application.
C# Application Exit Event:
Handle the application exit event using the ApplicationExit
event.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { Console.WriteLine("Application is exiting..."); // Cleanup or additional actions before exit } }
Handle Application Close Event in C#:
Handle the close event of a Windows Forms application.
using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { this.FormClosing += MainForm_FormClosing; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // Your logic before closing the application Console.WriteLine("Closing the application..."); } }
Closing Event in Windows Forms C#:
Use the FormClosing
event to handle the form closing in a Windows Forms application.
using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { this.FormClosing += MainForm_FormClosing; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // Your logic before closing the form Console.WriteLine("Closing the form..."); } }
Graceful Application Shutdown in C#:
Perform cleanup tasks before exiting the application.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { Console.WriteLine("Performing cleanup before application exit..."); // Cleanup tasks } }
Detecting Application Exit in C#:
Detect when the application is about to exit.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { Console.WriteLine("Application is exiting..."); // Additional actions before exit } }
Handling Form Closing Event in C#:
Handle the closing event of a form in a Windows Forms application.
using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { this.FormClosing += MainForm_FormClosing; } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // Your logic before closing the form Console.WriteLine("Closing the form..."); } }
Closing a Console Application in C#:
Use the Environment.Exit
method to close a console application.
using System; class Program { static void Main() { // Your application logic here // Close the application Environment.Exit(0); } }
Application.Exit Method in C#:
Use the Application.Exit
method to close a Windows Forms application.
using System; using System.Windows.Forms; class Program { static void Main() { // Your application logic here // Close the application Application.Exit(); } }
Prompt User on Application Exit C#:
Display a confirmation dialog before exiting the application.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you sure you want to exit?", "Exit Confirmation", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { Console.WriteLine("Exiting the application..."); // Cleanup or additional actions before exit } else { // Cancel exit } } }
Shutdown Hooks in C# Application:
Implement shutdown hooks for cleanup tasks before application exit.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { Console.WriteLine("Performing cleanup before application exit..."); // Cleanup tasks } }
Cancel Application Exit in C#:
Cancel the application exit based on certain conditions.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { // Check conditions before exit if (CanExit()) { Console.WriteLine("Exiting the application..."); // Cleanup or additional actions before exit } else { // Cancel exit Console.WriteLine("Exit canceled."); } } static bool CanExit() { // Check conditions for allowing exit return true; } }
Closing Main Window in WPF C#:
Handle the closing event of the main window in a WPF application.
using System; using System.Windows; class MainWindow : Window { public MainWindow() { this.Closing += MainWindow_Closing; } private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // Your logic before closing the main window Console.WriteLine("Closing the main window..."); } }
FormClosed Event in C#:
Use the FormClosed
event in a Windows Forms application.
using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { this.FormClosed += MainForm_FormClosed; } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { // Your logic after the form is closed Console.WriteLine("Form closed."); } }
Terminate Application in C#:
Terminate the application using Environment.Exit
.
using System; class Program { static void Main() { // Your application logic here // Terminate the application Environment.Exit(0); } }
Implementing Cleanup on Application Exit in C#:
Perform cleanup tasks before exiting the application.
using System; using System.Windows.Forms; class Program { static void Main() { Application.ApplicationExit += Application_ApplicationExit; // Your application logic here } static void Application_ApplicationExit(object sender, EventArgs e) { Console.WriteLine("Performing cleanup before application exit..."); // Cleanup tasks } }