Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-03-31
9
Medium Priority
?
1,226 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

971 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