By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,543 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,543 IT Pros & Developers. It's quick & easy.

Delay Windows Restart/Shutdown

P: n/a
Gurus,

I am trying to delay Windows Shutdown/Restart to perfrom cleanup and I
am using the following code:

[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name = "FullTrust")]

protected override void WndProc(ref Message ex)
{

if (ex.Msg == WM_QUERYENDSESSION)

{

//cancel the Shutdown/Restart

Message MyMsg = new Message();

MyMsg.Msg = WM_CANCELMODE;

base.WndProc(ref MyMsg);

//do the necessary work

//Run the Issued Restart/Shutdown command

base.WndProc(ref ex);

}

else

{

//send the message as normal

base.WndProc(ref ex);

}

}

But I realize that the cancel message is not being passed to other
windows if I do this and other applications start displaying error/
crash messages because they begin to shutdown. It looks like
'base.WndProc' sends message a message only after you come out of the
WndProc method and hence you cannot pass both WM_CANCELMODE and
Message ex within the same method call.

The Message 'ex' cannot be unfortunately used to distinguish between
Restart/Shutdown and I need to perform whatever was requested after
performing the required operations.

Please let me know if there are any alternatives.

Any help is greatly appreciated. Thanks in advance.
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pete,
Thanks for your reply.

WM_CANCELMODE message seems to cancel the shutdown if it is sent
alone.
And also WM_QUERYENDSESSION message seems to continue the windows
Shutdown, again, when sent alone.

My application basically stops other applications gracefully on the
system and is about 10 seconds of cleanup work.
The only problem that I have is that other applications get the
shutdown message at the same time as the main app and hence the main
app is not able to close some of the other applications that it starts
and monitors.

I'll meanwhile look at Application.SessionEnding event and see if I
could make use of it.

Do you have any other suggestions?
Jun 27 '08 #2

P: n/a
On Mon, 19 May 2008 05:10:08 -0700, IdleBrain <in**************@yahoo.com>
wrote:
Pete,
Thanks for your reply.

WM_CANCELMODE message seems to cancel the shutdown if it is sent
alone.
I can't explain this. Perhaps there's something inside the .NET default
window proc that signals Windows to interrupt the shutdown when it gets
the message. However, as you've seen...if you cancel the shutdown, you
can't just continue by later calling the base implementation of
WndProc(). That's not how the shutdown works.
And also WM_QUERYENDSESSION message seems to continue the windows
Shutdown, again, when sent alone.
But it's not the act of passing that message to the base WndProc() that
causes the shutdown to continue. It's simply that you haven't canceled
the shutdown. Absent an application canceling the shutdown, it will
proceed no matter what you do
My application basically stops other applications gracefully on the
system and is about 10 seconds of cleanup work.
The only problem that I have is that other applications get the
shutdown message at the same time as the main app and hence the main
app is not able to close some of the other applications that it starts
and monitors.
Why not? If it's because those applications have a prompt up, you could
try sending _those_ windows a WM_CANCELMODE. If it's because those
applications are already closed, then why is it a problem for it to not be
able to close them?

You've certainly entered in a gray area that IMHO illustrates the hazards
of trying to manage other processes from a given process. You're not
specific about the nature of the processes, so I don't know how much
control you have over them. But ideally you would be able to rewrite all
of the applications involved so that they coordinate their shutdown
process as necessary.

If the other applications are third-party applications without a
well-defined API for interacting with them, well...you're likely to have
problems, sorry to say.

Pete
Jun 27 '08 #3

P: n/a
If the other applications are third-party applications without a
well-defined API for interacting with them, well...you're likely to have
problems, sorry to say.
Yeah the other tools that my App is trying to control are third party
tools and I cannot cancel the shutdown individually from them.
I just donot understand why MS makes it hard to find out if the issued
message was a Shutdown/Restart.
My Life would have been much easier if I knew that !!

Thanks for your reply. I appreciate your time.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.