Click here to Skip to main content
Click here to Skip to main content

Make Your Application Shutdown Aware

, 27 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how to make your application shutdown aware




This article is not an out of the box article. However I feel this may bring in some more ideas in the field of application development because knowledge makes you think beyond limitation.


I was working with one application and my client wanted the application to be system shutdown aware. I remembered that lot many applications do some task based on closing of an application but rarely saw any application which differentiated user shutdown and system shutdown. I thought why not make people aware that there is something which may change in the application design.

I have seen a couple of applications which handle FormClosing event and show MessageBox of closing, but when system shutdown is requested, they still show the message and end up with either data loss or prevent system shutdown.

So I decided to put this in the form of an article.

Basic Idea

FormClosingEventArgs is derived from CancelEventArgs. FormClosingEventArgs has a property (CloseReason) which tells you the reason for closing. This may be used in identifying and handling the close reason.

Now you have two options to get this arguments during close.

  1. You can override OnClosing method declared in Form and cast CancelEventArgs e to FormClosingEventArgs:

    protected override void OnClosing(CancelEventArgs e) 
        //Your alternate implementation
        FormClosingEventArgs ce = e as FormClosingEventArgs; 
        if (ce != null) 
            //Handle ce
  2. You can use event FormClosing:

    private void SampleForm_FormClosing(object sender, FormClosingEventArgs e)
        //Your alternate implementation

Handle Close Scenario

switch (ce.CloseReason)
    case CloseReason.ApplicationExitCall:
        //Handle application exit call
    case CloseReason.FormOwnerClosing:
        //Handle Form owner close
    case CloseReason.MdiFormClosing:
        //Handle MDI parent closing
    case CloseReason.None:
        //Handle unknown reason
    case CloseReason.TaskManagerClosing:
        //Handle taskmanager close
    case CloseReason.UserClosing:
        Handle User close
    case CloseReason.WindowsShutDown:
        //Handle system shutdown

Points of Interest

If you go through the MSDN documentation, you will find the following details of CloseReason. The names itself are self explanatory:

  • ApplicationExitCall - The Exit method of the application class was invoked.
  • FormOwnerClosing - The owner form is closing.
  • MdiFormClosing - The parent form of this multiple document interface (MDI) form is closing.
  • None - The cause of the closure was not defined or could not be determined.
  • TaskManagerClosing - The Microsoft Windows Task Manager is closing the application.
    UserClosing - The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the Window's control menu, or pressing ALT+F4.
  • WindowsShutDown - The operating system is closing all applications before shutting down.


  • 28th January, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Manish Ranjan Kumar
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

GeneralIf only FormClosingEventArgs worked for PocketPC [modified] PinmemberUKCodeMonkey4-Jun-10 4:36 
Generalsome issue Pinmemberarif.setiawan24-Feb-10 15:10 
GeneralThis is also limited to applications using forms Pinmemberbjreed6-Feb-08 6:23 
GeneralRe: This is also limited to applications using forms PinmemberManish Ranjan Kumar8-Feb-08 21:55 
Questionclosereason is only helpful for legal shutdowns... what about illegal ones? Pinmemberti-oh28-Jan-08 20:32 

I know this is an old issue... But Id like to point out the same (old) hazard about closereason...

If you goto windows->taskmanager->application tab and end-task your App, then OnFormClosing will be performed... right, so far so good...

But, if you do windows->taskmanager->processes and end-process then you'd notice OnFormClosing event would not even come... no, no, not with the all powerful kill process...

It is here were detecting shutdown reasons for both legal and illegal sequences can make anybody's code inside OnFormClosing func do some greater good...

Lets say, id like to make an auto-recovery full-proof Application,
then we already know that detecting shutdown reasons at OnFormClosing would no longer suffice, since the App would not have the chance to decide stuff OnFormClosing...

Although there are ways to circumvent a kill process scenario(googling will also help)... But if not then let me know...

think fast, be brave and dont stop.

AnswerRe: closereason is only helpful for legal shutdowns... what about illegal ones? PinmemberKerkia20-Aug-08 17:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 28 Jan 2008
Article Copyright 2008 by Manish Ranjan Kumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid