Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-03-31
9
Medium Priority
?
1,205 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 1600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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