none
Block shutdown

Answers

  • You can use the CloseReason() property:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
     Handles MyBase.FormClosing
        If e.CloseReason() = CloseReason.WindowsShutDown Then
            e.Cancel() = True
        End If
    End Sub

    However, it is definitely not advised or even appropriate for an app to do this. Most of the time when the user goes to shut down they just want the computer to shut down. Clients certainly wouldn't like this feature I would imagine.
    As I said before, I never repeat myself.
    Saturday, August 01, 2009 12:27 PM
  • etisoppo,

    That will just only stop application from closing. Not OS. It will work because by default "Force Closing Not Responding Application" is not set. If it is set by user, ^ will fail.

    something like

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If e.CloseReason = CloseReason.WindowsShutDown Then
                Process.Start("shutdown.exe", "/a")
            End If
        End Sub

    should work, not tested.

    ------------------------------------------
    Edit by Martin Xie:
    Yes, the command "shutdown.exe -a" means aborts shutdown.
    Syntax: Shutdown [{-l|-s|-r|-a}] [-f] [-m [\\ComputerName]] [-t xx] [-c "message"] [-d[u][p]:xx:yy]
    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true


    Thanks
    -@omkarnath

    May the force be with you
    Saturday, August 01, 2009 7:37 PM

All replies

  • what do you mean shutdown? as closing the application with end?
    try the form closing for the application..

    and if you mean shutdown, as shuting down the computer, i can not help with that.. that is not an application that i am willing to try and modify.
    trujade.
    i like: VB General  google  fast cars  username password
    Saturday, August 01, 2009 10:55 AM
  • When you speak about shutdown, you are talking about the OS which is deeply concerned with policies and administrative rights. I believe it is an advanced topic even for experts.

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Saturday, August 01, 2009 11:02 AM
  • @trujade i meant shutting down the computer .If my application is running shutdown process should be aborted.Is that possible to block  ??
    Saturday, August 01, 2009 11:11 AM
  • i know windows 7 does something similar.
    if a file is not saved on some programs, when shutting down, if those programs are still open, it prompts for a force close, or i can get back to using the programs.. sometimes when it updates, it needs a restart, and if i am afk, then i could loose my work.

    i have not tried this, but i think this is something that might be of use.  try to work with the form closing.
    here' is something with one button on a form

     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If Button1.Text = "Button1" Then
                e.Cancel = True
                ' will not close application
            Else
                'close
                e.Cancel = False
            End If
        End Sub
    that blocks the application from closing, so it might block your shutdown and give you a prompt to force close it... not sure, not about to try either.. let me know..

    although Waleed El-Badry does make a good point, but loosing alot of work over some count down timer to shutdown if i am afk, is worth looking into.
    trujade..
    i like: VB General  google  fast cars  username password
    Saturday, August 01, 2009 11:27 AM
  • You can use the CloseReason() property:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) _
     Handles MyBase.FormClosing
        If e.CloseReason() = CloseReason.WindowsShutDown Then
            e.Cancel() = True
        End If
    End Sub

    However, it is definitely not advised or even appropriate for an app to do this. Most of the time when the user goes to shut down they just want the computer to shut down. Clients certainly wouldn't like this feature I would imagine.
    As I said before, I never repeat myself.
    Saturday, August 01, 2009 12:27 PM
  • well said etisoppo,
    e.CloseReason usgae in VB.NET
    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Saturday, August 01, 2009 1:04 PM
  • hello etisoppo ,
    actually this blocking shutdown should be detecting shutdown. This is an old concept the intention is warning user that the application should be closed before computer shuts down .... and if user says close application then close it or leave the computer on
    Saturday, August 01, 2009 7:25 PM
  • etisoppo,

    That will just only stop application from closing. Not OS. It will work because by default "Force Closing Not Responding Application" is not set. If it is set by user, ^ will fail.

    something like

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If e.CloseReason = CloseReason.WindowsShutDown Then
                Process.Start("shutdown.exe", "/a")
            End If
        End Sub

    should work, not tested.

    ------------------------------------------
    Edit by Martin Xie:
    Yes, the command "shutdown.exe -a" means aborts shutdown.
    Syntax: Shutdown [{-l|-s|-r|-a}] [-f] [-m [\\ComputerName]] [-t xx] [-c "message"] [-d[u][p]:xx:yy]
    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true


    Thanks
    -@omkarnath

    May the force be with you
    Saturday, August 01, 2009 7:37 PM
  • etisoppo is 100% correct, aborting a shut-down is definitely taboo. Usually, if a program/programs stop responding, the user will shut down the computer because usually, everything is closed. If a user runs into difficulties, the last thing they want it is to be aborted buy the app causing the problem.

    I am a developer, I make money off it and I know what my customers want and don't want. If I done this in my applications, there would be moans and groans. I have NEVER came across an application that stops a shutdown, and I really think that you should not stop it; it's poor design.

    Remember that Windows Vista and Windows Seven can automatically detect a program running during a shutdown and prompts the user to see if they want to continue with the shutdown or abort it safely. Let the user make the decision, not the other way ;-)



    Thanks, Callum Kerr. - - - Want software utilities? Check out my website for more info!
    Saturday, August 01, 2009 7:45 PM
  • just wanted to add.. i love this forum in far more ways than just getting a solution to a problem. this due to all the extra information added about good vb. bad of using a solution..  makes me feel even better about being a msdn member.
    this good vs. bad statement i just mentioned, when searching the net, i hardly find other forums with this advantage, of good vs. bad added to replies, mostly just ____ about trying to teach someone..  any time a search result ends up to a msdn link, i know that 90% of the time, my answer is there, plus extra goodies...
    . nice work guys.. i'll pat my self on the back for trying to keep this up as well...

    ...about the thread....

    etisoppo's code, works as i described in my post of using a button's text detected and not allowing an app exit. (not the code, just about keeping the data if not saved, although i was sooo close... lol.. i'm still a noob..  )

    by using his posted code in a built application, it is caught by windows 7, and prompted about a force shutdown or cancel..
    this force shut down prompt is by default in windows 7.  many users, as i, do not know how to set it "Force Closing Not Responding Application" as omie described, and will never bother to find out how, since this option is amazing for a default in a application, windows 7 being that application.. although omie, good information to pay attention to when working with such..

    ....off topic.....
    (ckerr15, i believe omie was the first i saw with that icon a while a go, then djpip showed around, and a reply from you with a 'grrrrrr' followed.. i'm certain you know what reply i am talking about.. you should 'grrrr' omie also.. (giggling) ;o)

    trujade.
    i like: VB General  google  fast cars  username password
    Saturday, August 01, 2009 9:34 PM
  • ...On the topic of an off topic topic... I don't know if that makes sense but anyway...

    I'm not too bothered about it, if Microsoft give you a grrrr then that is a cause for concern ;-) 

    Thanks, Callum Kerr. - - - Want software utilities? Check out my website for more info!
    Saturday, August 01, 2009 10:22 PM
  • (giggling) nope, no sense..
    i agree about the grrr from microsoft.... i love ms..

    grrr, lol.
    trujade.
    i like: VB General  google  fast cars  username password
    • Edited by •.trujade.• Sunday, August 02, 2009 7:35 AM grrr from no one
    Saturday, August 01, 2009 10:27 PM
  • Sorry about the no sense making (damn it). It's late here in Scotland, doing VB and watching Meet the Spartans at the same time is not a good mix :-(
    Thanks, Callum Kerr. - - - Want software utilities? Check out my website for more info!
    Saturday, August 01, 2009 10:31 PM
  • ckerr15.. i do not think you have read my post in one of the sticky notes..

    Some General Guidelines Using this Forum

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/a1f4e9aa-5b90-47e8-b0b1-21329499960d

    8. using improper language.. to be precise..

    i knew i posted that for a reason, that being, not to watch Meet the Spartans while replying to a thread.

    grrrrr...
    trujade.. lol.
    i like: VB General  google  fast cars  username password
    • Edited by •.trujade.• Saturday, August 01, 2009 11:07 PM i miss my firefox ;o/.. please fix the forum to be accessible properly by firefox.. i keep having missing and extra words in my replies with i.e.8
    Saturday, August 01, 2009 10:38 PM
  • lol.. just noticed my good vs. bad reply got a ___  and i did not put that there myself.. how in the world is someone's c.rap'ing is to be underlined and not their beaver dam buiding.  guess i better read my reply in the 'Some General Guidelines Using this Forum' thread also. lol..
    trujade.
    i like: VB General  google  fast cars  username password
    Saturday, August 01, 2009 11:04 PM
  • Fail!

    I noticed Firefox does not work, I am using Google Chrome, it works great! Give it a try, it beats nearly all browsers and I am very settled with it. I moved from IE to Opera, Firefox, Safari, Opera again then Chrome. It just keeps getting better and better! Chrome is the future.

    Thanks, Callum Kerr. - - - Want software utilities? Check out my website for more info!
    Saturday, August 01, 2009 11:14 PM
  • chrome.google.com
    Thanks, Callum Kerr. - Want software utilities? Check out my website for more info! Xbox Live Gamertag: callum151000
    Saturday, August 01, 2009 11:20 PM
  • (good thing this thread is answered in more ways than one)

    ckerr15, a few questions..
    does google chrome have all the addons as firefox?
    i love the google search from the firefox context menu after highlighting a word, as well as adblock for quick loading of webpages when trying to find a solution to a question for an op, and not really interested in viewing all the stuff about how to loose weight or fixing your grill.  ( i like bbq's, just was not reffering to that type of grill)
    if it was my question i needed an answer, i would use i.e. to view the website properly. glad it comes as a default in all windows o.s.'s.
    paste to tab and go, nice firefox addon.
    new tab homepage, (goes to google for me), i love.
    being able to drag the tab to a reply, also handy.
    no dropdown prompt menu like i.e for all the c. rap'ing when viewing a page or a clicked link.
    wot (world of trust) on all links, less spyware..
    and quite a few more addons..
    i am not willing to give up on firefox..
    i will however, see how google chrome acts and responds, but i doubt it will ever compare to firefox. do not down my firefox. grrrr.. lol

    ckerr15, do let me know about those addons.. i will download google chrome following this reply, but do advise on my questions.
    trujade..
    i like: VB General  google  fast cars  username password
    • Edited by •.trujade.• Monday, August 03, 2009 2:13 AM cleaned up some content for good reasons
    Saturday, August 01, 2009 11:47 PM
  • Well, Chome does not (currently) support any third party extensions, but supports just about every popular plugin. There is several things better than Firefox, but less not as good. I can do nearly everything Firefox can do, but more that it can't. I think Chrome outweighs Firefox which is why I am still with it. My browser loyalty is a difficult thing for me, and I am very satisified with Chrome. It is significantly faster than all browser, very stable, if it becomes unstable, it has always recovered.

    Really, Chrome is worth working out, it's bright, simple and minimal, everything you need is there, nothing much more or less than what you need. If you are on a laptop like me, smaller screen means less space, IE takes about 1/6 of my screen space, Chrome 1/10. 

    Thanks, Callum Kerr. Check out my website for information on my software products. Play Halo 3, I do! Xbox Live Gamertag: callum151000. I can do more than vb!
    Saturday, August 01, 2009 11:55 PM
  • wtf happened to all my nice icons on my toolbar.. they are all blank.. shooting blanks, no goood.. lol
    i worked hard to get an icon for my website, and getting blanks, read my above sentence.
    it does seem like a new toy to play with, inspect the element option, nice to learn html junk, and probably more.
    other than that, seems quick.

    one last chromed question, plugins.. how to access?
    trujade.

    just tried to post the above reply in chrome, got an error.. strike one for google chrome. the blank icons, strike 2.. one more, and no more chrome for me. final.

    i like: VB General  google  fast cars  username password
    Sunday, August 02, 2009 12:14 AM
  • WHEN THE DAY GOOGLE COMES TO TAKE OVER THE WORLD YOU WILL ALL SUBMIT TO CHROME! MWHAHAHAHAHAHAHA-*cough* that's not good for you. Shame you don't like it, it's always good to try :-)
    Thanks, Callum Kerr. Check out my website for information on my software products. Play Halo 3, I do! Xbox Live Gamertag: callum151000. I can do more than vb!
    Sunday, August 02, 2009 12:19 AM
  • cute cough.. too bad it was a real one.

    firefox has done me only good ever since i first tried it.. it slammed opera to the floor, kicked it out the door, and built a wall where the door was..

    i.e was there with opera, just my firefox let's i.e. do some odd jobs every now and then, just won't let it back in either.. kind of nice to have i.e mow the lawn, look at the grass grow, etc., still helps where needed..

    i guess i will just have to let i.e. do the sidework until my firefox can get back to helping out msdn.. i will be there a lot more than am now, to do as well.
    trujade.


    i like: VB General  google  fast cars  username password
    • Edited by •.trujade.• Sunday, August 02, 2009 12:30 AM yay... got a reply sent from chrome to msdn.. ?a.bunch of c rapping.. ;o)
    Sunday, August 02, 2009 12:28 AM
  • I'll be online tomorrow (2/08/09) It's 2:00AM here and I'm nodding off. I'll pick up replys tomorrow :-) Thanks!

    Thanks, Callum Kerr. Check out my website for information on my software products. Play Halo 3, I do! Xbox Live Gamertag: callum151000. I can do more than vb!
    Sunday, August 02, 2009 1:12 AM
  • You cannot, vb.net can only detect those things given by Microsoft to be used.

    To do what you want, you have to go in the OS and then you are simply a hacker.


    Success
    Cor
    Sunday, August 02, 2009 4:44 AM
  •  if Microsoft give you a grrrr then that is a cause for concern ;-) 
    Thanks, Callum Kerr. - - - Want software utilities? Check out my website for more info!

    i think i gave microsoft a grrrr. ;o)
    view following thread.
    <<<<<<<<<<<<<a href="nothing">deleted the thread, no link to link to</a>
    i just hope they don't grrr back.
    trujade.
    i like: VB General  google  fast cars  username password

    EDIT:
    i deleted my post from the 'suggestions and feedback forum' before i could get a grrrr from microsoft.. lol. got 43 views before doing so.
    dropped an a-bomb, let it stink for a minute, then ran out the door like i was never there..
    i'm sure they got a wiff of it though.. it was about fixing my firefox, and boy, did it stink.. saved some in a file, for later.
    trujade.
    • Edited by •.trujade.• Sunday, August 02, 2009 7:52 AM EDIT... and link deletion
    Sunday, August 02, 2009 5:33 AM
  • Am I mistaken or this thread suddenly deviated to a web browser comparison :-)

    For me I use Firefox. It is still the supreme browser for many users. Chrome is very promising. I think we shall wait till we see the new OS from Google.


    Cheers

    Waleed El-Badry Teaching Assistant Faculty of Engineering Misr University for Science & Technology
    Sunday, August 02, 2009 4:52 PM
  • what about when you have an app that is running on the background... that tells the boss login and loging-out times... and the nice user is forcing shutdown.. and with that your application goes bye bye...and with that no more loging out times recorded for the boss...so what do you do in that case.. how to know that win 7 is shutting down.. and before your process dies.. send that info to the web service....
    GR
    Wednesday, October 06, 2010 7:24 PM