An application is a server that simply starts until the system turns off or receives Ctrl + C or the console window is closed.
Due to the extraordinary nature of the application, it is impossible to "gracefully" exit. (Perhaps I could encode another application that would send a “shutdown” message to the server, but that would be redundant for one application and still not enough for certain circumstances, for example, when the server (actual OS) actually shuts down.)
Due to these circumstances, I added " ConsoleCtrlHandler ", where I stop my threads and clear my COM objects, etc.
Public Declare Auto Function SetConsoleCtrlHandler Lib "kernel32.dll" (ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As Boolean Public Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As Boolean Public Enum CtrlTypes CTRL_C_EVENT = 0 CTRL_BREAK_EVENT CTRL_CLOSE_EVENT CTRL_LOGOFF_EVENT = 5 CTRL_SHUTDOWN_EVENT End Enum Public Function ControlHandler(ByVal ctrlType As CtrlTypes) As Boolean . .clean up code here . End Function Public Sub Main() . . . SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ControlHandler), True) . . End Sub
This setting seems to work fine. Here is a link to some C # code for the same.
user79755 Jul 13 '09 at 15:09 2009-07-13 15:09
source share