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.