• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1247
  • Last Modified:

Detecting Logoff, Shutdown, or restart from tray icon app in VB.net

Can anyone help?

I have tried:
AddHandler SystemEvents.SessionEnding, AddressOf ShutdownApp
Private Sub ShutdownApp()
     Application.Exit()
End Sub

and:
Private Shared WM_QUERYENDSESSION As Integer = &H11
 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
         If m.Msg = WM_QUERYENDSESSION Then
                        systemShutdown = True
        End If
        MyBase.WndProc(m)
    End Sub

The first method works fine if i have a form open on the screen, but does not work if my application is minimised to tray.

The second method will shut down my application, but only if there is a form open. It aslo somehow cancels the Windows shutdown forcing the user to shutdown again.

How can I make my application shutdown gracefully from the system tray without interfering with the Windows shutdown?

Note: Vista compatability is preferred (Though I am currentlt testing on XP SP3) - I recall reading that Vista might have issues with the second method?

0
Rabbit80
Asked:
Rabbit80
  • 6
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How are you starting and keeping your app alive?  ...that may be the "root" cause of why your app doesn't close properly.

Are you canceling the closing of the main form and instead hiding it?  If so, then you may need code like this to allow it to close properly:
Public Class Form1
 
    ' This will hide the form when the "X" is pressed...
    ' ...but allow it to be closed programmatically with Me.Close() or when the system logs off/shuts down
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Const SC_CLOSE As Integer = &HF060&
        Const WM_SYSCOMMAND As Integer = &H112
 
        Select Case m.Msg
            Case WM_SYSCOMMAND
                Select Case m.WParam.ToInt32
                    Case SC_CLOSE
                        ' Close button "X" was pressed
                        
                        Me.Hide()
 
                        ' suppress default action 
                        Exit Sub ' keeps form open when "X" is clicked
 
                End Select
        End Select
 
        MyBase.WndProc(m)
    End Sub
 
End Class

Open in new window

0
 
Rabbit80Author Commented:
I have diabled the form controls (IE close button) and have a quit button and a minimise button on the form. Therefore I don't actually need to keep the form alive as such. The Minimise button of mine just calls the me.hide() and the notify icon routine. The exit button simply saves settings and calls application.exit().
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I have diabled the form controls (IE close button)"

Exactly...that may keep the app from closing properly depending how you wrote that part.  Use my code or show me your code instead...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rabbit80Author Commented:
Using your code then - how do I allow Application.Exits() from other parts of my program?
0
 
Rabbit80Author Commented:
Scratch that - I accidentally removed something else I shouldn't have!
0
 
Rabbit80Author Commented:
OK - this still isn't working.

It is not detecting the windows logoff/shutdown event at all I'm afraid!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"Using your code then - how do I allow Application.Exits() from other parts of my program?"

You can close your app programmatically with "Me.Close()" (from within the main form) and the code I posted will allow it out.  My example detects when the user clicks on the "X" in the top right and instead hides it.

I'm afraid we would need to see more of your code to figure out the problem.  Otherwise we can only post examples of what we know to work...
0
 
Rabbit80Author Commented:
Ok - I'm making some progress. My application is now exiting as it should. I changed the &H112
to &H16 which is WM_ENDSESSION

But.... Windows is still not logging off / shutting down after my application has exited!
0
 
Rabbit80Author Commented:
OK - I have finally got it - the reason Windows wasn't logging off was because my app wasnt replying to the WM_QUERYENDSESSION. I have modified my code as below:

Note the m.Result = True
Const WM_QUERYENDSESSION As Integer= &H11
 
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
     Select Case m.Msg
          Case WM_QUERYENDSESSION
                sSave() ' Sub for saving settings
                Application.Exit()
                m.Result = True
     End Select
     MyBase.WndProc(m)
End Sub

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now