i want to my application can prevents from windows shut down. i know that there is a system command to do that. but don't work for my program. i use this code for "cancel" the windows shut down:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
{
MessageBox.Show("Cancelling Windows shutdown");
string cmd = "shutdown /a";
Process.Start(cmd);// for executing system command.
}
}
and also use this code, but does not work :( :
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnding += SessionEndingEvtHandler;
}
private void SessionEndingEvtHandler(object sender, SessionEndingEventArgs e)
{
MessageBox.Show("Cancelling Windows shutdown");
string cmd = "shutdown /a";
Process.Start(cmd);// for executing system command.
}
i would be grateful if anyone explain me how can in "cancel" windows shutdown. thanks
This is strongly ill advised and Microsoft make it as hard as possible to do this. If a user wants to shut down, then it is the user's responsiblity, not the applications. As per the Microsoft article Application Shutdown Changes in Windows Vista:
Silent shutdown cancellations will no longer be allowed
In Windows XP, applications are allowed to veto WM_QUERYENDSESSION without displaying any UI indicating why they need to cancel shutdown. These "silent shutdown failures" are highly frustrating to users, who often take a minute or two to realize that shutdown has failed because no UI was displayed.
Windows Vista will eliminate this possibility by displaying UI even if an application vetoes WM_QUERYENDSESSION.
...also...
Applications should not block shutdown
If you take only one thing away from reading this topic, it should be this one. You will be presenting the best experience to your users if your application does not block shutdown. When users initiate shutdown, in the vast majority of cases, they have a strong desire to see shutdown succeed; they may be in a hurry to leave the office for the weekend, for example. Applications should respect this desire by not blocking shutdown if at all possible.
If you really do need to intevene during shutdown, there is a new API which you should register with:
Using the New Shutdown Reason API
The new shutdown reason API consists of three functions:
BOOL ShutdownBlockReasonCreate(HWND hWnd, LPCWSTR pwszReason); BOOL ShutdownBlockReasonDestroy(HWND hWnd); BOOL ShutdownBlockReasonQuery(HWND hWnd, LPWSTR pwszBuff, DWORD *pcchBuff);
Again, the best practice for Windows Vista applications at shutdown is that they should never block shutdown. However, if your application must block shutdown, Microsoft recommends that you use this API.
But at the end of the day, all this will do is present a user interface to the user to say that application is preventing shutdown and asking the user if they want to continue and force shutdown anyway. If they answer yes, you can't block this, and there is no way to block the UI.
Read the MSDN article I've linked to - it explians the model from Vista onwards. Ultimately, the paradigm is one of giving users control, and preventing applications overriding user demands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With