Search code examples
windowsvb.netshutdownformclosing

.NET Canceling closing of a form prevents it from closed on Windows shutdown


 Private Sub frmMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = True
    Me.WindowState = FormWindowState.Minimized
End Sub

Hello!

I am using this simple code above. However if the application is open while I am shutting down the computer, Windows waits until it's closed or wants me to terminate it in order to continue. I couldn't find a way to know if the user is trying to close application or the Windows is. All I know is, in both situations Windows sends close message to the window and this doesn't really help me. I can think of some other ways yet there should be a "clear" way of knowing.

Thanks in advance :)


Solution

  • You should be able to implement a close reason enumeration from this list. This will allow you to make it so that your computer will shutdown. You will have to implement the WindowsShutDown and/or TaskManagerClosing enumeration into your e.CloseReason.