views:

80

answers:

2

I am writing an application that runs in the background from startup to shutdown, and in some circumstances I need the application to display a dialogue for the user to choose whether or not to continue shutting down. This application will only be running on Windows, but may be running on any version from 2000 onward.

While I have certainly done some research on the topic, the information regarding the way that windows handles application shut down (http://msdn.microsoft.com/en-us/library/ms700677(VS.85).aspx) seems to be a bit scattered. If anyone has an example of the best way to handle this, or additional knowledge regarding the topic, I would very much appreciate it (I have very little experience with C#).

Thanks!!

badPanda

Edit: I am aware that blocking shutdown is not considered to be good practice. It is a critical business requirement for this application.

Edit: On suggestion of the several commentators below (to redesign so that blocking shutdown is unnecessary), I have utilized a NotifyIcon Balloon popup to inform users not to shut down when the application is sending data. While this is not AS likely to prevent data from being held up on a local computer for weeks, I feel that it is a good compromise between the best practice and the business requirements. After all, if users will be stupid...well, users will be stupid.

+2  A: 

Well, the MSDN article you linked explains it clearly: you cannot prevent Windows from shutting down if your app is running on Vista or Win7. Prompting the user is not going to work, the dialog will be inaccessible. You will have to consider an app design that deals with this.

Hans Passant
Actually, according to the article, it is possible to block shutdown as long as the new API for handling shutdown is implemented. (Or at least this is how I read it. Please let me know if I am incorrect.)
badpanda
No. I recommend you try this. Note the Force button.
Hans Passant
In some situations (not most, but while a specific method is running), it is absolutely business critical. The only other option is to request that the user restart their computer to allow the application to finish the task.This is because it is serving customers in an industry where the computers are second priority, and often not started for weeks at a time, but the task must complete in a timely manner.
badpanda
Are you not able to modify the method so that, if shutdown is initiated, the application will create a log message and will either roll back whatever it's doing, if it's sending data of some sort elsewhere, or will in some other way fail in a safe manner? Shutdowns are sometimes unavoidable. In fact, what happens when a computer your application is running on loses power due to a blackout or a cable disconnecting/cord coming unplugged? The application obviously won't be able to prevent the computer turning off in that situation even if the OS APIs properly supported it.
JAB
I do have an error log, however, I need to be able to handle the situation where the shutdown by the user is intentional. Since the user has no other interaction with the application, and it runs in the background, it is important that they at least be aware that this method has not finished running. It would complete itself upon restarting the computer, but it is common for the computers to be off for weeks, and the completion of the method is critical to business systems (for example, payroll).
badpanda
You cannot reliably enforce business critical security policies with a program. The sys admin needs to remove the SeShutdownPrivilege right from the user account. Ask questions about it at serverfault.com
Hans Passant
+2  A: 

I agree with JAB. A programmatic solution can't be used to solve hardware failure. Look into setting domain permissions to disallow shutdown's. To be effective, you may also need to restrict hardware access. You can also add UPS's or get all kinds of $$ with hot site backup.

This really sounds like a job for a transaction based thin client application.

P.Brian.Mackey
Sorry, I must not be doing a very good job of clarifying myself. This situation has absolutely nothing to do with the case of hardware failure. Restricting user access to shutting down is absolutely not an option. The application simply needs to ask the user whether or not they wish to continue shutting down, given that this method is still running. Most of the time, it will not be an issue. The only reason this needs to be implemented is because if the users are not aware of the issue, they will have absolutely no idea that their information was not sent.
badpanda