Link to home
Start Free TrialLog in
Avatar of jazduck
jazduck

asked on

Windows wont Shutdown when running my App

I have a slight problem - Windows wont shut down when its running my app.  The code which affects it is below, the only problem is the same line of code is what stops my app closing when the [X] is clicked, so i can place it in the systemtray.  Any info as to why this happens, or a workaround would be really helpful.

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True    '<---- Culprit (Wont Allow Windows to Shutdown)  
    HideToSysTray()
End Sub

As an afterthought could probably set e.Cancel = False, if it's possible to detect when windows wants to shutdown.

Jaz
Avatar of jazduck
jazduck

ASKER

Solved this one myself.  My afterthought was the way to go.

For any interested the code to detect a shutdown, and thus decide whether or not to close the app is as follows:

    Private Shared QueryShutdown As Integer = &H11
    Private Shared systemShutdown As Boolean = False

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = QueryShutdown Then
            systemShutdown = True
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If (systemShutdown) Then
            systemShutdown = False
            e.Cancel = False
        Else
            e.Cancel = True
            HideToSysTray()
        End If
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial