?
Solved

VB.Net  Pausing Windows Shutdown to confirm closing of application

Posted on 2015-02-06
4
Medium Priority
?
608 Views
Last Modified: 2015-02-07
Hi Experts.  I have an application with a lot of data that runs minimized.  If the PC is shutdown unknowingly, there is the possibility of lost data.  I would like to ensure that when the user shuts down the PC with the app still running , that the shutdown process is paused while the user is asked if all data is saved or whatever with the option to return to the app or continue to shutdown.

I've read a lot of nonsense about that fact that we shouldn't be upsetting the user be interfering with the shutdown process, but I see this behaviour is already present in some apps in W7 and for some apps it is very sensible.

There doesn't seem to be a definitive method that I could find

Many thanks in advance for any assistance
0
Comment
Question by:PNRT
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Scobber earned 1600 total points
ID: 40595059
In your form source code, select the event for form.formclosing, there should be a attribute for cancel as Boolean,

set cancel to true, and this will abort the form closure.

you may want to modify your source to add a variable to check whether you have asked the form to close so it will not need to be forced shut

in VB2013 the following code works
  
Private Sub frmMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
End Sub

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 40595220
This one has changed in recent versions of Windows. Personally, I have not seen the behavior you describe for a long time. Even Office 2003 applications do not display their old "Do you want to save" message anymore when you shutdown the OS.

See Shutdown Changes for Windows Vista. The discussion is mostly for C and C++ programmers that use the Windows API directly, but in my experience, it holds for Windows Forms.

It used to be that applications could use code similar to the one presented by Scobber. But, according to a Microsoft blog I have seen a couple of years ago, there was a problem with that exact code. It could prevent Windows from closing and the user did not know why. The right procedure (which I am sure Scobber use in real code) is to first inform the user of the situation, and ask him if he wants to Cancel the shutdown or not.

What happens now, as explained in the reference I gave you and in my experience with most applications, including Windows Form applications that use  FormClosing, is that when the user calls a Shutdown or a Restart, the OS automatically takes control. You get the little turning "thing" with the Shutting Down message, and then, a list of applications that are trying to prevent Windows from Closing. You can Cancel from there.

This is nice to prevent badly written applications from stopping Windows from closing without giving any information to the user, but this is bad for applications, because if you display a confirmation message in FormClosing, it does display... but only if the user Cancel from the OS list of applications that hold the system. Because the user does not know why such and such application prevents the Shutdown, they usually do not activate Cancel, do not see your nice informative message, and they lose their work.

As described in the Microsoft explanation for the change, it seems that by going through the Windows API you might be able to display your own message to the user in the page displayed by the OS, calling the ShutdownBlockReasonCreate API function. You might search for it to try to find code that do it in VB.
0
 
LVL 2

Author Closing Comment

by:PNRT
ID: 40596100
Scobber's suggestion worked fine.   Windows advised that the app was not shutting down and gave you the chance to cancel and go back to the app or force the shutdown
Thanks for the help
0
 
LVL 7

Expert Comment

by:Scobber
ID: 40596243
James, there is a reason why office applications don't always display the message. Windows will instruct the office application that it is rebooting, the office application saves the state of the document and closes.
Windows in turn will startup office on reboot and reload the document for the user. So save prompt on quit is not entirely necessary for smarter applications
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question