Solved

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

Posted on 2009-03-31
9
1,107 Views
Last Modified: 2013-11-07
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
Comment
Question by:Rabbit80
[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
  • 6
  • 3
9 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 24029408
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
 
LVL 1

Author Comment

by:Rabbit80
ID: 24029899
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24030440
"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
Industry Leaders: 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!

 
LVL 1

Author Comment

by:Rabbit80
ID: 24038062
Using your code then - how do I allow Application.Exits() from other parts of my program?
0
 
LVL 1

Author Comment

by:Rabbit80
ID: 24038074
Scratch that - I accidentally removed something else I shouldn't have!
0
 
LVL 1

Author Comment

by:Rabbit80
ID: 24038315
OK - this still isn't working.

It is not detecting the windows logoff/shutdown event at all I'm afraid!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24039786
"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
 
LVL 1

Author Comment

by:Rabbit80
ID: 24039940
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
 
LVL 1

Accepted Solution

by:
Rabbit80 earned 0 total points
ID: 24071913
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

631 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