• 0

[vb .net] app prevents windows shutdown


Question

my program, when running, prevents windows from shutting down and I know it's because I have a swicth statement in my closing event but I'm not sure how to fix the error, here is my current code:

   Private Sub formMain_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            Select Case mainMinOnClose
                Case True
                    e.Cancel = True
                    Me.WindowState = FormWindowState.Minimized
                Case False
                    e.Cancel = False
                    Call CloseApp()
            End Select
        Catch a As Exception
        End Try
    End Sub

with closeapp being this:

   Private Function CloseApp()
        Call xmlfile.saveSettings()
        Call SaveNotes()
        sysTrayIcon.Dispose()
        Dim count As Integer = 0
        Do While count < noteform.Count
            noteform(count).noteTrayIcon.dispose()
            count = count + 1
        Loop
        End

    End Function

so how can i fix this so windows will shutdown right?

Link to comment
https://www.neowin.net/forum/topic/106564-vb-net-app-prevents-windows-shutdown/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Just because i'm nice to you i'll show u how to do it in vb.net

Add the following in your main form

? ?
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
 ? ? ? ? ? ?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

Now in your form closing event

then now u need to add a IF Statement wrapping around your e.cancel code

here's what u do

IF (systemShutdown) = True Then

Call CloseApp()

Else


 ? ? ?Try
 ? ? ? ? ? Select Case mainMinOnClose
 ? ? ? ? ? ? ? Case True
 ? ? ? ? ? ? ? ? ? e.Cancel = True
 ? ? ? ? ? ? ? ? ? Me.WindowState = FormWindowState.Minimized
 ? ? ? ? ? ? ? Case False
 ? ? ? ? ? ? ? ? ? e.Cancel = False
 ? ? ? ? ? ? ? ? ? Call CloseApp()
 ? ? ? ? ? End Select
 ? ? ? Catch a As Exception
 ? ? ? End Try


End If

Hope that helps

  • 0

Hate to drag this topic up again, especially since I gave bad info before. :blush:

I stumbled across this today. There are events under the Microsoft.Win32 namespace, SystemEvents class. SessionEnding and SessionEnded events. If you handle SessionEnding, you can get the reason, and possibly cancel the event, tho it isn't guaranteed.

A little easier than overriding wnproc... well, maybe not easier, but more .NET-ish. There are some other cool events, like DisplaySettingsChanged, which could come in handy.

  • 0
  weenur said:
Hate to drag this topic up again, especially since I gave bad info before. :blush:

I stumbled across this today. There are events under the Microsoft.Win32 namespace, SystemEvents class. SessionEnding and SessionEnded events. If you handle SessionEnding, you can get the reason, and possibly cancel the event, tho it isn't guaranteed.

A little easier than overriding wnproc... well, maybe not easier, but more .NET-ish. There are some other cool events, like DisplaySettingsChanged, which could come in handy.

hmm im going to have to look into that because the fix you gave me doesnt work when restarting my system, only when shutting down.

  • 0
  weenur said:
Hate to drag this topic up again, especially since I gave bad info before. :blush:

I stumbled across this today. There are events under the Microsoft.Win32 namespace, SystemEvents class. SessionEnding and SessionEnded events. If you handle SessionEnding, you can get the reason, and possibly cancel the event, tho it isn't guaranteed.

A little easier than overriding wnproc... well, maybe not easier, but more .NET-ish. There are some other cool events, like DisplaySettingsChanged, which could come in handy.

Hey John, nah the session ending namespace isn't the 100% working way to do it, i looked into that before as well, my method i posted above is the real way that MSDN sports, here's the link to the article

http://msdn.microsoft.com/library/default....endingtopic.asp

  • 0
  Winston said:
Hey John, nah the session ending namespace isn't the 100% working way to do it, i looked into that before as well, my method i posted above is the real way that MSDN sports, here's the link to the article

Yeah, there are some issues with using some of the events in that namespace. Namely the SessionEnding and SessionEnded events (they don't always fire in the right order or at the appropriate time, and using them for cleanup is a BAAAAD idea). The WndProc and/or PreProcessMessage overrides is a better way to do it.

--

Danny Smurf

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.