C# Cancel Windows Shutdown -


i want application can prevents windows shut down. know there system command that. don't work program. use code "cancel" 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);// executing system command.     } } 

and use code, 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);// executing system command.   } 

i grateful if explain me how can in "cancel" windows shutdown.

this ill advised , microsoft make hard possible this. if user wants shut down, user's responsiblity, not applications. per microsoft article application shutdown changes in windows vista:

silent shutdown cancellations no longer allowed

in windows xp, applications allowed veto wm_queryendsession without displaying ui indicating why need cancel shutdown. these "silent shutdown failures" highly frustrating users, take minute or 2 realize shutdown has failed because no ui displayed.

windows vista eliminate possibility displaying ui if application vetoes wm_queryendsession.

...also...

applications should not block shutdown

if take 1 thing away reading topic, should one. presenting best experience users if application not block shutdown. when users initiate shutdown, in vast majority of cases, have strong desire see shutdown succeed; may in hurry leave office weekend, example. applications should respect desire not blocking shutdown if @ possible.

if need intevene during shutdown, there new api should register with:

using new shutdown reason api

the new shutdown reason api consists of 3 functions:

bool shutdownblockreasoncreate(hwnd hwnd, lpcwstr pwszreason); bool shutdownblockreasondestroy(hwnd hwnd); bool shutdownblockreasonquery(hwnd hwnd, lpwstr pwszbuff, dword *pcchbuff); 

again, best practice windows vista applications @ shutdown should never block shutdown. however, if application must block shutdown, microsoft recommends use api.

but @ end of day, present user interface user application preventing shutdown , asking user if want continue , force shutdown anyway. if answer yes, can't block this, , there no way block ui.

read msdn article i've linked - explians model vista onwards. ultimately, paradigm 1 of giving users control, , preventing applications overriding user demands.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -