Page 1 of 1

Something similar to #PB_Event_CloseWindow for a console?

Posted: Sun Jan 28, 2024 4:25 pm
by Quin
The server for my networking application runs as a console. It all works fine, even working as a systemd service on my actual server. When running it from source on Windows to test something though, I noticed that it would apparently crash if I hit alt+f4. The status bar wouldn't show any errors, nothing would show up as an error line, but it would say the debug executable quit unexpectedly. The main loop of the application is a Repeat : ForEver loop that calls NetworkServerEvent() every iteration, and nothing else. I'm wondering if I need to capture some sort of console closed event and shut down more cleanly. I'm not sure when this started so it's entirely possible it's something super weird in the code, but I want to rule this out first. Is there a method for capturing when the console closes, whether that be with control+c, alt+f4, etc?
Thanks.

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Sun Jan 28, 2024 5:05 pm
by mk-soft
The control command Ctrl-C can be intercepted and ignored. Alt-F4 (Close) can be queried but not cancelled.
MSDN: https://learn.microsoft.com/en-us/windo ... r-function

Code: Select all

Procedure myConsoleHandler(dwControlType.l)
  Protected r1
  Select dwControlType
    Case #CTRL_C_EVENT                      ; CTRL+C signal
      Debug "CTRL+C pressed"
      r1 = #True
    Case #CTRL_BREAK_EVENT                  ; CTRL+BREAK signal
      Debug "CTRL+BREAK pressed"
      r1 = #True
    Case #CTRL_CLOSE_EVENT                  ; Close button pressed or End Task in TaskManager
      Debug "Close console signal"
      r1 = #False
    Case #CTRL_LOGOFF_EVENT                 ; user is logging off
      Debug "User is logging off"
      r1 = #False
    Case #CTRL_SHUTDOWN_EVENT               ; system is shutting down
      Debug "System shutdown signal"
      r1 = #False
  EndSelect
  ProcedureReturn r1
EndProcedure

DisableDebugger

If OpenConsole()
  
  SetConsoleCtrlHandler_(@myConsoleHandler(), #True) ; register handler
  
  Repeat
    PrintN("Exit with 'q'")
    
    a$ = Input()
  Until a$ = "q"
  
  SetConsoleCtrlHandler_(@myConsoleHandler(), #False) ; un-register handler
  
  CloseConsole()
EndIf

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Sun Jan 28, 2024 7:48 pm
by jacdelad
Oh come on, will this be asked weekly now?

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Mon Jan 29, 2024 8:25 pm
by Quin
jacdelad wrote: Sun Jan 28, 2024 7:48 pm Oh come on, will this be asked weekly now?
Perhaps if something is asked a lot, it would be good to document it somewhere, not show up in random topics with the snark turned up to 100, no?

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Mon Jan 29, 2024 8:46 pm
by jacdelad
https://www.purebasic.fr/english/viewtopic.php?t=66591
https://www.purebasic.fr/english/viewtopic.php?p=475353
https://www.purebasic.fr/english/viewtopic.php?t=21334
https://www.purebasic.fr/english/viewtopic.php?t=82733
https://www.purebasic.fr/english/viewtopic.php?t=83399
...and so on. While there could be another sentence in the help about that, you can find numerous information asking Google with the keywords "purebasic", "console" and "close"/"prevent closing"/"alt f4","ctrl c"...
...and yes, I'm rude. Sorry.

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Mon Jan 29, 2024 9:01 pm
by Quin
jacdelad wrote: Mon Jan 29, 2024 8:46 pm https://www.purebasic.fr/english/viewtopic.php?t=66591
https://www.purebasic.fr/english/viewtopic.php?p=475353
https://www.purebasic.fr/english/viewtopic.php?t=21334
https://www.purebasic.fr/english/viewtopic.php?t=82733
https://www.purebasic.fr/english/viewtopic.php?t=83399
...and so on. While there could be another sentence in the help about that, you can find numerous information asking Google with the keywords "purebasic", "console" and "close"/"prevent closing"/"alt f4","ctrl c"...
...and yes, I'm rude. Sorry.
I also should've definitely worded my question better, because it extended slightly beyond just wanting a way to determine when someone tried to close it. My question was more intended to ask if there was a way to gracefully shut down a console app, because I'm experiencing some weird behavior where control+c or alt+f4 displays the debug executable quit unexpectedly, but no line is focused with an error message or anything. My application is a server that runs on a Repeat...ForEver loop, and my thought is that some connection isn't closed or it tries to do something with the console window, which was now closed, but this is the first time I've ever seen anything like it, so I frankly have no idea.

Re: Something similar to #PB_Event_CloseWindow for a console?

Posted: Mon Jan 29, 2024 9:36 pm
by jacdelad
There's an API to bend the app shutdown via close button to an own function, but I don't remember it (Google should help) and finally after calling the function, the app will shutdown anyway.
The console is not designed to be a companion to your app, but to control it and control it hard by the user, if needed (forced close, whatever the program is doing right now). With this in mind, the console is not always suitable.
There's a simulated, customizable console somewhere here in the forum, maybe this one helps you.