share
Super UserDisable automatic termination of applications on shutdown
[+8] [3] Lawand
[2009-08-19 14:05:27]
[ windows-xp shutdown ]
[ https://superuser.com/questions/25655/disable-automatic-termination-of-applications-on-shutdown ]

Is there any Windows XP alternative to Windows Vista's shutdown process which prompts the user whether to continue or cancel shutdown in case some programs contain an unsaved data?

[+10] [2009-08-22 20:31:51] Scott Weinstein [ACCEPTED]

You can do this with some code by handing the SystemEvents.SessionEnding [1] event. This will show a dialog box when you try to logoff or shutdown and ask if you want to cancel the logoff or shutdown.

The code can be compiled for free with either the Visual C# 2008 Express Edition [2] or with the windows SDK [3].

With the sdk, use the following command:

csc.exe   /out:StopShutdown.exe /target:winexe StopShutdown.cs 

Here's the code:

using System;
using System.Windows.Forms;
using Microsoft.Win32;

namespace StopShutdown
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
           string desktopRegKey = @"HKEY_CURRENT_USER\Control Panel\Desktop";
           Registry.SetValue(desktopRegKey, "AutoEndTasks", 0);
           Registry.SetValue(desktopRegKey, "WaitToKillAppTimeout", 20000);
           Registry.SetValue(desktopRegKey, "HungAppTimeout", 20000);

            Form AppForm = new Form()
                {
                    ClientSize = new System.Drawing.Size(0, 0),
                    ControlBox = false,
                    FormBorderStyle = FormBorderStyle.None,
                    Opacity = 0,
                    ShowIcon = false,
                    ShowInTaskbar = false,
                    SizeGripStyle = SizeGripStyle.Hide,
                };

            SystemEvents.SessionEnding += (_e, e) =>
            {
                DialogResult dr = MessageBox.Show(
                                    "Cancel shutdown?"
                                    , "Shutdown",
                                    MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1);

                e.Cancel = (dr == DialogResult.Yes);
            };


            Application.Run(AppForm);
        }

    }
}

Edit:

Downloadable source and exe [4].

[1] http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
[2] http://www.microsoft.com/express/vcsharp/
[3] http://msdn.microsoft.com/en-us/windows/bb980924.aspx
[4] http://cid-552753bb951aae20.skydrive.live.com/self.aspx/.Public/Code%20Samples/StopShutdown.zip

would you be a nice person and upload the "StopShutdown.exe" somewhere or email it to me? (I would understand if you said no) - Lawand
Good work, you got really close: I left an unsaved file open then I invoked shutdown from start menu, next a prompt asked me if I want to cancel shutdown but when I waited for a couple of seconds, Windows terminated your program and carried out the shutdown process... - Lawand
Yes, if you want XP to wait longer, you'll need to modify the registry values mentioned in another post. I'll update the code... - Scott Weinstein
Actually I want XP to halt the shutdown process permanently (like Vista does). Anyway your answer is the closest so far... - Lawand
1
[+2] [2009-08-19 17:35:44] Breakthrough

If you're willing to do a little registry editing... Start -> Run -> regedit

HKEY_CURRENT_USER\Control Panel\Desktop

Make sure AutoEndTasks is 0, and set WaitToKillAppTimeout to 20000 (the default value of 2 seconds). You can set the value higher if you wish. There's also HungAppTimeout (the defalt is 5000), but that applies more for applications which are not responding.


This gives more time for apps to be closed properly, but it doesn't permanently stop the shutdown process... - Lawand
2
[0] [2009-08-23 02:11:05] warren

Whenever I do a shutdown on XP, if a program is a busy, it gives me a progress bar and an option to 'End Now' or 'Cancel'.

Clicking 'Cancel' stops the shutdown process. However, whatever it has already shutdown doesn't come back up.

But it does give me time to save what I was working on before re-attempting the shutdown.


But if a program is responding (not busy) it will be forced to close even if it contained unsaved data - Lawand
that's not what you asked - if it's not responding, it doesn't matter if you're trying to shutdown or not, you'll lose unsaved data - warren
Sorry, I shouldn't have said "busy programs", I meant programs that contain an unsaved data and that are not busy... - Lawand
(1) oh, then my answer still stands - at least on plain-vanilla installs of XP: any time I've done a shutdown but had, for example, an unsaved doc in Word, I had the opportunity to cancel the shutdown, save my doc(s), and then restart the shutdown process - warren
Couple of days ago I installed XP SP3 and now the system does what you say, whereas with SP2 it didn't... - Lawand
weird - it did with me on original Xp, SP1, SP2, and SP3 :) - warren
3