Solved

VB.Net  Pausing Windows Shutdown to confirm closing of application

Posted on 2015-02-06
4
487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Scobber earned 400 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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