Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
OnShutdown event in a Windows Service
Message
From
28/03/2014 10:10:27
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01597504
Message ID:
01597550
Views:
53
Here is a simplified version of the code. The problem is that the Closing() event of the form is not called:
Partial Class HiddenForm
    Inherits Form

    Private oIContainer As System.ComponentModel.IContainer = Nothing
    Private Shared WM_QUERYENDSESSION As Integer = &H11
    Private Shared lSystemShutdown As Boolean = False

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        SuspendLayout()
        AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
        AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        ClientSize = New System.Drawing.Size(0, 0)
        FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Name = "HiddenForm"
        Text = "HiddenForm"
        WindowState = System.Windows.Forms.FormWindowState.Minimized
        ResumeLayout(False)
    End Sub

    Protected Overrides Sub WndProc(ByRef toMessage As System.Windows.Forms.Message)

        ' If this is a logoff, shutdown or reboot
        If toMessage.Msg = WM_QUERYENDSESSION Then
            MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot")
            lSystemShutdown = True
        End If

        ' If this is WM_QUERYENDSESSION, the closing event should be raised in the base WndProc
        MyBase.WndProc(toMessage)

    End Sub

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

        ' Michel Fournier
        lSystemShutdown = True

        ' If this is a shutdown
        If lSystemShutdown Then

            ' Reset the variable because the user might cancel the shutdown
            lSystemShutdown = False

            If MessageBox.Show("My application", "Do you want to save your work before logging off?", MessageBoxButtons.YesNo) = _
             System.Windows.Forms.DialogResult.Yes Then
                e.Cancel = True
            Else
                e.Cancel = False
            End If

        End If

    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

        If Disposing AndAlso Not (oIContainer Is Nothing) Then
            oIContainer.Dispose()
        End If

        MyBase.Dispose(Disposing)
    End Sub

End Class
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform