?
Solved

VB.Net  Pausing Windows Shutdown to confirm closing of application

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 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