Solved

VB.Net  Pausing Windows Shutdown to confirm closing of application

Posted on 2015-02-06
4
342 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 400 total points
Comment Utility
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 100 total points
Comment Utility
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 1

Author Closing Comment

by:PNRT
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now