Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Application exit event - WinForms

Tags:

c#

winforms

Is this possible to capture Windows form close event when clicked on application stop. I know application stop working but is there any method available who will fire in any case when application is closing? I found these methods from google but they are not working:

 AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);  

or

  Application.ApplicationExit += new EventHandler(this.OnApplicationExit); 

Is it possible to get close event when click on application stop button?

like image 545
user1387147 Avatar asked May 14 '12 07:05

user1387147


People also ask

How do I exit WinForms application?

The proper method would be Application. Exit() .

Is WinForms dying?

Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.

Does Microsoft still support WinForms?

"We continue to support and innovate in Windows Forms runtime," said Microsoft's Igor Velikorossov last month in announcing what's new for WinForms in . NET 6. He's a software engineer on the dev team for the 19-year-old product, a free and open-source graphical (GUI) class library included as a part of .


1 Answers

In Program.cs file, you must have those methods before Application.Run :

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        Application.Run(new MainForm());
    }

You can also use the Disposed event on the MainForm (the form used in Application.Run method).

like image 77
kerrubin Avatar answered Sep 19 '22 10:09

kerrubin