Using code to hook the console close event from this thread, I sometimes get a NullReferenceException with no stacktrace (most of the times I don\'t). It happen
For the vb.net bods who are struggling with this, my code is ...
'declaration
Private Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal handlerRoutine As ConsoleEventDelegate, ByVal add As Boolean) As Boolean
Public Delegate Function ConsoleEventDelegate(ByVal [event] As ConsoleEvent) As Boolean
'The close function...
Public Function Application_ConsoleEvent(ByVal [event] As ConsoleEvent) As Boolean
Console.WriteLine("We're closing it all down: ")
Return False
End Function
'creating the handler.
If Not SetConsoleCtrlHandler(New ConsoleEventDelegate(AddressOf Application_ConsoleEvent), True) Then
Console.WriteLine("Unable to install console event handler.")
Exit Sub
End If