none
Temporarily Prevent Shutdown/Logoff RRS feed

  • Question

  • What would be the best method to prevent a shutdown/logoff temporarily? 

    When a shutdown or log off is initiated by the user I would like to prompt the user with the option to take a survey before he session is ended. Would this be possible? I have tried using the SessionEndingEventHandler but this has not seemed to work for me


    Matthias

    Wednesday, January 2, 2013 4:32 PM

Answers

  • What I meant is launch notepad, enter few words and shutdown Windows. You'll see that :
    - as you quit, notepad asks to save your work with a modal window (as it would be done whenever its window closes with unsaved data)
    - as this app blocks, Windows ask to confirm or cancel the shutdown

    So you could likely do the same in the FormClosing event so that the user can choose to save, cancel or don't save survey results. In FormClosing you also have a e.CloseReason that could allow to display something particular to shutdown if really needed.

    My understanding is that your application is NOT responsible directly to cancel the shutdown, it just does whatever you want and if you block by displaying a modal dialog, Windows will take care of showing the shutdown cancellation screen.

    Else you should perhaps ask in a Windows API, C++ forum as this is more about how Windows works at a quite low level rather than  really a C# question.

    Ah. It reminds me I saw once that shutdown.exe have an option to cancel shutdown (try http://support.microsoft.com/kb/317371/en-us and the /l/a/y switches). Double check this is really what you want as it seems to me it would produce a somewhat unusual experience if ti really works.

    If the goal is that this survey is the only application that could run and will terminate the session itself, the "kiosk mode" could perhaps fit your needs : http://answers.microsoft.com/en-us/windows/forum/windows_7-desktop/how-can-i-setup-a-computer-with-windows-7-to-be/89f3246f-baaf-45fc-b946-07450475a5b0


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, January 7, 2013 5:59 PM

All replies

  • Hi,

    You could try :

    http://bartdesmet.net/blogs/bart/archive/2006/10/25/Windows-Vista-_2D00_-ShutdownBlockReasonCreate-in-C_2300_.aspx

    Since Vista, an application can provide a reason for which it currently blocks shutting down Windows. This reason is then shown to the user on shutdown so that he can take an informed decision.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Wednesday, January 2, 2013 5:50 PM
  • Thanks for the reply Patrice.

    I have looked into method already and would like to display a Windows Form dialog before reaching that point. Would this be possible? 


    Matthias

    Wednesday, January 2, 2013 5:57 PM
  • Yes, I think so, please check this thread for more detailed sample codes to realize it, http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/21f87287-56e3-4327-8fb7-5c94167b1653/

    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 3, 2013 9:16 AM
    Moderator
  • I have tried this method prior to posting my problem here and this produces the same results for me. I would like to halt the shutdown/logoff, display a dialog asking if the user would like to take a survey. If yes, then open a window for the user to take the survey. If no, resume shutdown/logoff.

    It would seem it is not possible to halt the shutdown/logoff process.

    EDIT: I guess this is not possible in Vista and later http://msdn.microsoft.com/en-us/library/ms700677(v=VS.85).aspx

    I expect the method you have recommended Michael will work, however that post is from 2005 thus the XP era. According to the link above it seems that silent cancellations are no longer possible.


    Matthias


    • Edited by Matthiassb Thursday, January 3, 2013 2:54 PM
    Thursday, January 3, 2013 2:38 PM
  • IMO silent cancellation would be problematic if you want the user to stay in control...

    It's curious this is specifically for a shutdown. Couldn't you just ask the question if the application closes (wether or not this is caused by a shutdown ?). For example see how a usual app behaves when you shutdown and have not closed the document...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, January 3, 2013 8:32 PM
  • Hi Patrice,

    Thank you for the reply and I apologize for the delay in my response. It's not a survey for usage of a specific application. It's a survey that will contain questions that span a variety of questions/topics.

    This survey will be used on public machines in an educational institution.

    As it seems, silent cancellation is not allowed. Would you happen to have an idea of how one might be able to achieve a similar result without silent cancellation? Or could you further elaborate on your suggestion above? It seems slightly unclear to me.


    Matthias

    Monday, January 7, 2013 2:31 PM
  • What I meant is launch notepad, enter few words and shutdown Windows. You'll see that :
    - as you quit, notepad asks to save your work with a modal window (as it would be done whenever its window closes with unsaved data)
    - as this app blocks, Windows ask to confirm or cancel the shutdown

    So you could likely do the same in the FormClosing event so that the user can choose to save, cancel or don't save survey results. In FormClosing you also have a e.CloseReason that could allow to display something particular to shutdown if really needed.

    My understanding is that your application is NOT responsible directly to cancel the shutdown, it just does whatever you want and if you block by displaying a modal dialog, Windows will take care of showing the shutdown cancellation screen.

    Else you should perhaps ask in a Windows API, C++ forum as this is more about how Windows works at a quite low level rather than  really a C# question.

    Ah. It reminds me I saw once that shutdown.exe have an option to cancel shutdown (try http://support.microsoft.com/kb/317371/en-us and the /l/a/y switches). Double check this is really what you want as it seems to me it would produce a somewhat unusual experience if ti really works.

    If the goal is that this survey is the only application that could run and will terminate the session itself, the "kiosk mode" could perhaps fit your needs : http://answers.microsoft.com/en-us/windows/forum/windows_7-desktop/how-can-i-setup-a-computer-with-windows-7-to-be/89f3246f-baaf-45fc-b946-07450475a5b0


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Monday, January 7, 2013 5:59 PM