Making a Windows Forms App Respond to System Shutdown

Normally when Windows is shutting down, every top level window gets asked to close, and once they’re all closed, the system can shut down. Some applications, however, don’t exit when you close their main window – they hide the main window and leave a NotifyIcon icon on the system tray. When you double-click on the tray icon, the main window is made visible again.

This is how MSN Messenger works, for example, and many other applications.

The problem here is that when Windows wants to shut down, it sends you the close message, but it looks exactly the same as when the Close button is clicked by the user. You hide your window, you don’t quit, and the system doesn’t shut down.

To get around this, you need to detect a system shutdown event specifically.

There’s a message that you get sent when the system is shutting down, WM_QUERYENDSESSION. As much as I hate using native Win32 functions or message handling in a Windows Forms app, there doesn’t appear to be any other way to catch a system shutdown event than to use this.

I tried registering a notificaiton with SystemEvents.SessionEnding, which should have done the job, but if your main window is hidden, you don’t receive it.

So here’s what I ended up using:

//
// Watch for WM_QUERYENDSESSION, set the
// reallyQuit flag and quit
//
protected override void WndProc(ref Message m)
{
  base.WndProc(ref m);

  // WM_QUERYENDSESSION
  if (m.Msg == 0×11)
  {
    reallyQuit = true;
    Close();
  };
}

And then only allow the Closing event if reallyQuit is set. This seems to get the job done.

3 Responses to “Making a Windows Forms App Respond to System Shutdown”

  1. beelzeboris Says:

    Thank you so much for this – I’ve been cursing SystemEvents.SessionEnding for ages, and never could figure out why my tray app wouldn’t play nicely. Cheers.

  2. e Says:

    I’m pretty sure the FormClosing event includes a FormClosingEventArgs (or something like that) that tells you the reason for closure, e.g. button click or system shutdown. This would avoid digging into Win32 non-portability.

  3. Poul Says:

    e.CloseReason can be used. Contains 3 when hitting the “X” in the corner and 1 when logoff (and shutdown) is asked.

    Create a “public close_bool” as boolean = True

    Change all “e.cancel = True” to “e.cancel = close_bool”

    Call a public sub in all “FormClosing” events. In the Sub

    close_bool = False

    enumerate all forms and issue the .Close for all of them

    end it with Me.Close

Leave a Reply

buy cheap Endep online buy cheapest Endep and Endep how to order Amitriptyline online without prescription buy Amitriptyline without rx from us pharmacy online Amitriptyline purchase purchase online prescription Amitriptyline without purchasing Amitriptyline online without prescription order generic Amitriptyline buy Endep no prescriptions how to order Endep online without prescription comprare Endep generico Endep online buy Endep in england buy cheap Endep online free consult Endep purchased online without prescription what is Endep Endep tabletten buy Endep no perscription cod Endep non rx fedex overnight free want to buy Anafranil in usa Aricept overdose buy discount Aricept on line Aricept buy Aricept online Aricept order buy Aricept pills Nizoral suppliers cheap order rx Nizoral canada Nizoral order Nizoral usa cod comprar Nizoral generico buy Nizoral 200 mg buy cheap Nizoral under without rx buy cheap Nizoral with dr. prescription Prozac 200 mg buy Lisinopril australia el Nizoral generico buy Lisinopril tablets without rx next day delivery Lisinopril with no script buy Lisinopril c o d purchase arimidex no scams order Arimidex order amex Arimidex canadian pharmacy buy Arimidex without doctor order rx free Arimidex buy Abilify with american express buy Abilify cod order Finpecia without a prescription cheap order rx Finpecia purchase Famvir no visa online without prescription buy Famvir paypal without rx buy Famvir without Famvir without prescription medications Famvir without persription purchase Famvir online no membership overnight shipping Famvir no physician approval Amitriptyline purchase order Amitriptyline free next day airAmitriptyline on line Valtrex no rx needed cod accepted buy next day Fluoxetine buy Fluoxetine line purchase Fluoxetine cod delivery buy Fluoxetine 20 mg purchase Fluoxetine buy cheap Fluoxetine under without rx purchase Fluoxetine visa without prescription Fluoxetine by mail Fluoxetine for pets Fluoxetine 10mg Amitriptyline purchase online how to order Famvir online without a rx Prozac Amitriptyline no perscription Famvir next day purchase Anafranil no prescription cheap buy cheapest Anafranil next day delivery on Anafranil saturday Anafranil buy Anafranil buy Anafranil overnight where to purchase Anafranil no prescription no fees Anafranil drug buy Lisinopril where Lisinopril buy on line order Lisinopril online no membership overnight shipping no prescription required for Lisinopril order maxalt cash on delivery Maxalt from canada buy cheapest Maxalt purchase Maxalt visa without prescription buy Maxalt uk Maxalt 10 mg purchase Maxalt no visa online without prescription purchase generic Lisinopril online Lisinopril no prescription to buy maxalt order online no membership overnight how to get a arimidex rx Aricept without a perscription fedex Aricept overnight without a prescription Donepezil prescription order buy genuine Aricept Aricept uk sales prescription Proscar buy Proscar pay cod Maxalt apotheke buy no perscription Proscar purchase Proscar online buy cheap fedex Prednisone maxalt with no presciption buy Maxalt shipped cod Proscar online Prednisone order buy Amitriptyline with amex Accutane 40 mg delivered overnight buy brand Accutane 40 mg Accutane 40 mg orderd online without prescription where to purchase generic Accutane 40 mg online without a rx how to get 40 mg Accutane without next day delivery 40 mg Accutane with no script buy Amitriptyline online Accutane 40 mg online no prescription buy Accutane 40 mg online us pharmacy Accutane 40 mg best buy buy Accutane 40 mg c o d order 40 mg Accutane cash on delivery no presciption 40 mg Accutane