Close application when computer is shutting down...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I have written an application that sits in the systray and monitors a database for me. The problem is that this application is stopping some of my clients computers from shutting down, I assume because the app is still running when the computer is trying to shut down.

What I need to know is if there is a way that my application can tell when the computer is shutting down, so that I can close my application and therefore allow the computer to shut down.

I am only running my app on Windows O/S e.g. 2000, XP
The application is written in VS 2005, .NET 2 and VB.Net language

Anyway help would be really apreshiated

Thanks in advance

Simon
 
Hello.

Normally your app will receive a shutdown signal if the guest OS shuts down.
What is your app doing? Are you using somekind of endless loop with Thread.Sleep? If yes, try considering a Timer instead.

Most likely is the reason some sort of loop which prevents the application from processing any other 'things'.

Bobby
 
If your Main Form is open, but hidden, then its Closing event will fire with the reason for the close when the OS tries to quit it. See FormClosingEventArgs Class (System.Windows.Forms) and the CLoseReason enumeration

If the reason is that the system is rebooting, you're recommended not to cancel the closing event.

If your app doesnt have anything in the closing or closed event handlers then it is unlikely that your app is to blame for the system not shutting down provided that Robert Zenz' advice regarding NOT performing long running jobs or Sleeps on the UI thread is followed.

Do not block the UI thread, and do not cancel any attempts to close your app if you do not wish for it to interfere with the reboot
 
Had the exact same thing happening in one of my apps. I have it set so if the user clicks the close button it actually will cancel the close and go to a notification icon in the system tray. Then if they click a actual exit program button it sets a variable called confirm close which will allow it to close. Problem is on system shutdown/reboot my program gets the notification to close and then ignores it. Had to add this to my closing event:

PHP:
        If ConfirmClose = False And Not ((e.CloseReason = CloseReason.TaskManagerClosing) Or (e.CloseReason = CloseReason.WindowsShutDown)) Then
            e.Cancel = True
            Call DisplayNotifyIcon()
            Exit Sub
        End If

So in other words if they didnt close my program properly through a exit button AND were not being shut down by a windows shutdown or task manager close then we cancel the close and show the icon. Otherwise it closes like normal.
 
Back
Top