[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB.Net  Pausing Windows Shutdown to confirm closing of application

Posted on 2015-02-06
4
Medium Priority
?
567 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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