Delay Windows Restart/Shutdown

I

IdleBrain

Gurus,

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



[System.Security.Permissions.PermissionSet(System.Security.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.
 
I

IdleBrain

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?
 
I

IdleBrain

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top