Solved

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

Posted on 2009-03-31
9
1,025 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
  • 6
  • 3
9 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
"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
 
LVL 1

Author Comment

by:Rabbit80
Comment Utility
Using your code then - how do I allow Application.Exits() from other parts of my program?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

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

Author Comment

by:Rabbit80
Comment Utility
OK - this still isn't working.

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

Expert Comment

by:Mike Tomlinson
Comment Utility
"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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now