Console Cancel Key Press (CONTROL+C/Break)

AddHandler Console.CancelKeyPress, AddressOf myCtronCHandler


    Sub myCtronCHandler (ByVal sender As Object,

                                 ByVal args As ConsoleCancelEventArgs)

        funDeleteRegKey(arguments(1))

        'debug.print(vbCrLf & "The read operation has been interrupted.")

        'debug.print("  Key pressed: {0}", args.SpecialKey)

        'debug.print("  Cancel property: {0}", args.Cancel)

        '' Set the Cancel property to true to prevent the process from terminating.

        'debug.print("Setting the Cancel property to true...")

        'args.Cancel = True

        '' Announce the new value of the Cancel property.

        'debug.print("  Cancel property: {0}", args.Cancel)

        'debug.print("The read operation will resume..." & vbCrLf)

    End Sub

 Session Ending - Great with Control+C - Quit with Clean up

'https://samjenkins.com/detect-windows-shutdown/

Private allowClose As Boolean = False

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'This bit of code adds the event handler that deals with closing down the application when Windows shuts down or logs off
    AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SessionEnding
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not allowClose Then
        Me.WindowState = FormWindowState.Minimized
        Me.Visible = False
        e.Cancel = True
    Else
        'We allow the form to close
    End If
End Sub

Private Sub SessionEnding(ByVal sender As System.Object, ByVal e As Microsoft.Win32.SessionEndingEventArgs)
    allowClose = True
    Application.Exit()
End Sub
'https://itecnote.com/tecnote/net-closing-function-in-a-vb-net-console-application/