Disable automatic termination of applications on shutdown

8

4

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?

Lawand

Posted 2009-08-19T14:05:27.313

Reputation: 610

Answers

10

You can do this with some code by handing the SystemEvents.SessionEnding 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 or with the windows SDK.

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.

Scott Weinstein

Posted 2009-08-19T14:05:27.313

Reputation: 734

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 – 2009-08-22T20:55:01.447

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 – 2009-08-23T00:25:57.837

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 – 2009-08-23T02:24:04.173

Actually I want XP to halt the shutdown process permanently (like Vista does). Anyway your answer is the closest so far... – Lawand – 2009-08-27T11:59:12.110

2

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.

Breakthrough

Posted 2009-08-19T14:05:27.313

Reputation: 32 927

This gives more time for apps to be closed properly, but it doesn't permanently stop the shutdown process... – Lawand – 2009-08-19T18:04:29.197

0

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.

warren

Posted 2009-08-19T14:05:27.313

Reputation: 8 599

But if a program is responding (not busy) it will be forced to close even if it contained unsaved data – Lawand – 2009-08-23T22:18:03.793

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 – 2009-08-24T00:03:05.953

Sorry, I shouldn't have said "busy programs", I meant programs that contain an unsaved data and that are not busy... – Lawand – 2009-08-24T10:38:50.137

1oh, 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 – 2009-08-24T10:58:12.517

Couple of days ago I installed XP SP3 and now the system does what you say, whereas with SP2 it didn't... – Lawand – 2009-09-22T13:18:23.583

weird - it did with me on original Xp, SP1, SP2, and SP3 :) – warren – 2009-09-23T01:02:23.637