Home All Groups Group Topic Archive Search About

Application doesn't close on Shut Down - Please help!

Author
23 Jan 2006 2:09 PM
pamelafluente
Hi Guys,

I have a small application which uses a NotifyIcon. The user can set a
flag (PreventClosing ) so that when he clicks on the form-cancel button
"X", the  program will instead be minimized in the icon try. See code
attached.

My problem is that my code is probably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does NOT
want to shut down!  I guess it's the e.cancel = TRUE statement in the
closing handler that prevents it.

Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?

Thank you very much in advance,

-Pam

---------------------------------------------------------------------------­--



    Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed


    Private ShowInTaskbarSaved As Boolean
    Private WindowStateSaved As FormWindowState
'...
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Me.PreventClosing Then
            Me.HideInTrayIcon()
            e.Cancel = True
        End If
    End Sub

    Private Sub HideInTrayIcon()
        Me.WindowStateSaved = Me.WindowState
        Me.WindowState = FormWindowState.Minimized
        Me.ShowInTaskbar = False
        Me.NotifyIcon1.Visible = True
    End Sub

'...
    Private Sub RestoreWindow()
        Me.NotifyIcon1.Visible = False
        Me.ShowInTaskbar = Me.ShowInTaskbarSaved
        Me.WindowState = Me.WindowStateSaved
        Me.Show()
        Me.Focus()
    End Sub

----------------------

thank you in advance

-Pam

Author
23 Jan 2006 2:31 PM
Chris
pamelaflue***@libero.it wrote:
Show quoteHide quote
> Hi Guys,
>
> I have a small application which uses a NotifyIcon. The user can set a
> flag (PreventClosing ) so that when he clicks on the form-cancel button
> "X", the  program will instead be minimized in the icon try. See code
> attached.
>
> My problem is that my code is probably too naive. In fact there is a
> problem when one tries to shut dow or restart Windows. Windows does NOT
> want to shut down!  I guess it's the e.cancel = TRUE statement in the
> closing handler that prevents it.
>
> Could anybody by so kind as to suggest me the correct approach to do
> this. Is there a way to detect the the closing is due to system restart
>  or shutdown?
>
> Thank you very much in advance,
>
> -Pam
>
> ---------------------------------------------------------------------------­--
>
>
>
>     Private PreventClosing As Boolean
> 'if this is set TRUE by the user, the program minimizes (instead of
> closing) when "X" is pressed
>
>
>     Private ShowInTaskbarSaved As Boolean
>     Private WindowStateSaved As FormWindowState
> '...
>     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
> System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
>         If Me.PreventClosing Then
>             Me.HideInTrayIcon()
>             e.Cancel = True
>         End If
>     End Sub
>
>     Private Sub HideInTrayIcon()
>         Me.WindowStateSaved = Me.WindowState
>         Me.WindowState = FormWindowState.Minimized
>         Me.ShowInTaskbar = False
>         Me.NotifyIcon1.Visible = True
>     End Sub
>
> '...
>     Private Sub RestoreWindow()
>         Me.NotifyIcon1.Visible = False
>         Me.ShowInTaskbar = Me.ShowInTaskbarSaved
>         Me.WindowState = Me.WindowStateSaved
>         Me.Show()
>         Me.Focus()
>     End Sub
>
> ----------------------
>
> thank you in advance
>
> -Pam
>

You may want to look at this site.  It has info on handling win32 events.

http://www.codeworks.it/net/Sysevents.htm

You should be able to use it to get notified that the system (or
session) wants to shutdown, and close your program.


Never tried it, but in my head, it should work.
Chris
Author
23 Jan 2006 3:11 PM
pamelafluente
Dear Chris,

Thank you VERY much for your suggestion. I have read the
implementation, trying to do what you seems to suggest (hope I have
correctly interpreted).
See attached code changes.

There is however a big problem. The CLOSING event is called BEFORE
SystemEvents.SessionEnding. So the System Shut Down is blocked before I
can detect the session is being shutting down!   :-(

What can I do ? Thank you very much for your patience !!

-Pam

--------------

Imports Microsoft.Win32


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

        '....
        AddHandler SystemEvents.SessionEnding, AddressOf
Me.SessionEnding

    End Sub


    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        MsgBox("Program Closing")   'here we get!

        If Not Me.SessionIsEnding AndAlso Me.PreventClosing Then
            Me.HideInTrayIcon()
            e.Cancel = True
        Else
            Me.NotifyIcon1.Visible = False
        End If

    End Sub

    Private SessionIsEnding As Boolean

    Public Sub SessionEnding(ByVal sender As Object, ByVal e As
SessionEndingEventArgs)
        MsgBox("SessionEnding")      'never get here :-(
        Me.SessionIsEnding = True
    End Sub
--------------------------------------------

thank you very much,

-Pam
Author
24 Jan 2006 12:14 AM
Del
I answered this question a few days ago. Of couse you can ommit the
MessageBox & just let your application close on Windows shutdown

Private Shared WM_QUERYENDSESSION As Integer = &H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_QUERYENDSESSION Then
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or
reboot")
        systemShutdown = True
    End If
    ' If this is WM_QUERYENDSESSION, the closing event should be fired in
the base WndProc
    MyBase.WndProc(m)
End Sub 'WndProc


Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If (systemShutdown) Then
    ' reset the variable since they may cancel the shutdown
        systemShutdown = False
        If (DialogResult.Yes = _
                MessageBox.Show("My application", "Would you care to save
your work before logging off?", MessageBoxButtons.YesNo)) Then
                e.Cancel = True
        Else
                e.Cancel = False
        End If
    End If
End Sub

I hope that helps

Crouchie1998
BA (HONS) MCP MCSE
Author
24 Jan 2006 9:01 AM
pamelafluente
Thank you VERY VERY much Del !! It works perfectly and is exactly what
I needed.

Del ha scritto:

> I answered this question a few days ago. Of couse you can ommit the
> MessageBox & just let your application close on Windows shutdown

Ah. Please, excuse me. I can't read this fantastic group regularly

> Private Shared WM_QUERYENDSESSION As Integer = &H11
....
> I hope that helps

Very much!

>
> Crouchie1998
> BA (HONS) MCP MCSE

See you soon.

-Pam